Skip to content
This repository has been archived by the owner on Jan 27, 2019. It is now read-only.

Commit

Permalink
#14 Added possibility to register unknown VCS Repository Providers to…
Browse files Browse the repository at this point in the history
… support custom ones.
  • Loading branch information
blaubaer committed Mar 25, 2017
1 parent 0bbeaa4 commit 011680f
Show file tree
Hide file tree
Showing 22 changed files with 416 additions and 360 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ golang {
// Directory where to cache all dependencies in.
dependencyCache = 'vendor' // Path
// Method to register other VCS Repository provider that are not already supported by this plugin.
// vcsType: One of 'git', 'bzr', 'hg' or 'svn'
// prefix: Dependency prefix that identifies this provider.
// IMPORTANT: Add a slash as suffix.
// name: Name of this provider for display in logs etc.
// dependencyPattern: Regular expression (Java compatible) that should match the whole dependency and
// provides a submatch named <root> which matches the base of the dependency
// and another submatch named <subPath> that matches potential sub paths.
vcsRepositoryProvider('<vcsType>', '<prefix>', '<name>', '<dependencyPattern>')
// Example:
// vcsRepositoryProvider('git', 'github.com/', 'GitHub', '^(?<root>github\\.com/[A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+)(?<subPath>/[A-Za-z0-9_.\\-]+)*\$')
}
build {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '2.6.2'

// VCS
compile group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '4.4.1.201607150455-r'
compile group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '4.6.1.201703071140-r'

// Platform
compile gradleApi()
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Dec 21 10:57:06 CET 2016
#Sat Mar 25 13:34:53 CET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-all.zip
17 changes: 10 additions & 7 deletions samples/simple-project/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import org.echocat.gradle.plugins.golang.tasks.GolangTask
import org.echocat.gradle.plugins.golang.utils.Executor
import org.slf4j.*
/*
apply plugin: 'org.echocat.golang-minor'
apply plugin: 'org.echocat.golang'
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath "org.echocat.gradle.plugins:gradle-golang-plugin:0.1.15-SNAPSHOT"
classpath "org.echocat.gradle.plugins:gradle-golang-plugin:0.1.16-SNAPSHOT"
}
}
*/
} */


plugins {
id "org.echocat.golang" version "0.1.14"
id "org.echocat.golang" version "0.1.16"
}

group 'github.com/echocat/gradle-golang-plugin/samples/simple-project'
Expand All @@ -33,7 +33,10 @@ golang {
]
}
dependencies {
vcsRepositoryProvider("git", "try.gogs.io/", "Gogs Demo Repository", "^(?<root>try\\.gogs\\.io/[A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+)(?<subPath>/[A-Za-z0-9_.\\-]+)*\$")

test 'gopkg.in/check.v1'
build 'try.gogs.io/rblaubaer/blaubaers-foo'
tool 'github.com/golang/lint/golint'
tool 'github.com/jstemmer/go-junit-report'
}
Expand All @@ -43,8 +46,8 @@ golang {
}
}

task moo(type: MooTask, dependsOn: golangTest)
golangBuild.dependsOn moo
task moo(type: MooTask, dependsOn: test)
build.dependsOn moo

class MooTask extends GolangTask {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import static org.echocat.gradle.plugins.golang.DependencyHandler.GetResult.downloaded;
import static org.echocat.gradle.plugins.golang.model.GolangDependency.Type.*;
import static org.echocat.gradle.plugins.golang.model.GolangDependency.newDependency;
import static org.echocat.gradle.plugins.golang.model.VcsRepositoryProvider.toConcrete;
import static org.echocat.gradle.plugins.golang.utils.Executor.executor;
import static org.echocat.gradle.plugins.golang.vcs.CombinedVcsRepositoryProvider.delegatesWithDefaults;
import static org.echocat.gradle.plugins.golang.vcs.VcsRepository.Utils.progressMonitorFor;

public class DependencyHandler {
Expand All @@ -57,8 +59,7 @@ public boolean accept(Path path) {
};

@Nonnull
private final VcsRepositoryProvider _vcsRepositoryProvider = new CombinedVcsRepositoryProvider();

private final VcsRepositoryProvider _vcsRepositoryProvider;
@Nonnull
private final ProgressLoggerFactory _progressLoggerFactory;
@Nonnull
Expand All @@ -71,6 +72,8 @@ public DependencyHandler(@Nonnull ServiceRegistry serviceRegistry, @Nonnull Sett
public DependencyHandler(@Nonnull ProgressLoggerFactory progressLoggerFactory, @Nonnull Settings settings) {
_progressLoggerFactory = progressLoggerFactory;
_settings = settings;
final List<VcsRepositoryProvider> delegates = toConcrete(settings.getDependencies().getVcsRepositoryProviders());
_vcsRepositoryProvider = new CombinedVcsRepositoryProvider(delegatesWithDefaults(delegates));
}

@Nonnull
Expand All @@ -92,7 +95,8 @@ public Map<GolangDependency, GetResult> get(@Nonnull GetTask task) throws Except
final RawVcsReference reference = dependency.toRawVcsReference();
final VcsRepository repository = _vcsRepositoryProvider.tryProvideFor(reference);
if (repository == null) {
throw new RuntimeException("Could not download dependency: " + reference);
throw new IllegalArgumentException("Don't know how to handle dependency '" + reference + "' because there VCS Repository Provider that could handle it.\n" +
"\t\tTry register it under golang.dependencies with vcsRepositoryProvider(...).");
}
final String normalizedReferenceId = repository.getReference().getId();
if (!handledReferenceIds.contains(normalizedReferenceId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@
import javax.inject.Inject;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.echocat.gradle.plugins.golang.Constants.VENDOR_DIRECTORY_NAME;
import static org.echocat.gradle.plugins.golang.vcs.VcsType.git;
import static org.gradle.util.ConfigureUtil.configure;

public class DependenciesSettings {

protected static final Pattern NOTATION_PATTERN = Pattern.compile("(?<group>[a-zA-Z0-9.\\-_/]+)(?::(?<version>[a-zA-Z0-9.\\-_/]+))?");

private final boolean _root;

@Nonnull
Expand All @@ -33,6 +38,8 @@ public class DependenciesSettings {
private Boolean _deleteAllCachedDependenciesOnClean;
private Path _dependencyCache;

private Collection<VcsRepositoryProvider> _vcsRepositoryProviders;

@Inject
public DependenciesSettings(boolean root, @Nonnull Project project) {
_root = root;
Expand Down Expand Up @@ -75,7 +82,42 @@ public void setDependencyCache(Path dependencyCache) {
_dependencyCache = dependencyCache;
}

protected static final Pattern NOTATION_PATTERN = Pattern.compile("(?<group>[a-zA-Z0-9.\\-_/]+)(?::(?<version>[a-zA-Z0-9.\\-_/]+))?");
public Collection<VcsRepositoryProvider> getVcsRepositoryProviders() {
return _vcsRepositoryProviders;
}

public void setVcsRepositoryProviders(Collection<VcsRepositoryProvider> vcsRepositoryProviders) {
_vcsRepositoryProviders = vcsRepositoryProviders;
}

@Nonnull
public VcsRepositoryProvider vcsRepositoryProvider(@Nonnull String prefix, @Nonnull String name, @Nonnull String dependencyPattern) {
return vcsRepositoryProvider(prefix, name, Pattern.compile(dependencyPattern));
}

@Nonnull
public VcsRepositoryProvider vcsRepositoryProvider(@Nonnull String prefix, @Nonnull String name, @Nonnull Pattern dependencyPattern) {
return vcsRepositoryProvider(git, prefix, name, dependencyPattern);
}

@Nonnull
public VcsRepositoryProvider vcsRepositoryProvider(@Nonnull String type, @Nonnull String prefix, @Nonnull String name, @Nonnull String dependencyPattern) {
return vcsRepositoryProvider(VcsType.valueOf(type), prefix, name, Pattern.compile(dependencyPattern));
}

@Nonnull
public VcsRepositoryProvider vcsRepositoryProvider(@Nonnull VcsType type, @Nonnull String prefix, @Nonnull String name, @Nonnull Pattern dependencyPattern) {
final VcsRepositoryProvider result = new VcsRepositoryProvider(type, prefix, name, dependencyPattern);
add(result);
return result;
}

public void add(@Nonnull VcsRepositoryProvider vcsRepositoryProvider) {
if (_vcsRepositoryProviders == null) {
_vcsRepositoryProviders = new ArrayList<>();
}
_vcsRepositoryProviders.add(vcsRepositoryProvider);
}

@Nonnull
public Dependency add(@Nonnull String configurationName, @Nonnull Object notation) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.echocat.gradle.plugins.golang.model;

import org.echocat.gradle.plugins.golang.vcs.VcsType;
import org.echocat.gradle.plugins.golang.vcs.isps.DefaultVcsRepositoryProvider;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
import static java.util.regex.Pattern.compile;
import static org.echocat.gradle.plugins.golang.vcs.VcsType.git;

@Immutable
public class VcsRepositoryProvider {

@Nonnull
private static final List<VcsRepositoryProvider> DEFAULTS = unmodifiableList(asList(
new VcsRepositoryProvider(git, "github.com/", "GitHub", compile("^(?<root>github\\.com/[A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+)(?<subPath>/[A-Za-z0-9_.\\-]+)*$")),
new VcsRepositoryProvider(git, "gopkg.in/", "gopkg.in", compile("^(?<root>gopkg\\.in(?<repo>(?:/[A-Za-z0-9_.\\-]+){1,2})\\.v(?<version>[0-9]{1,9}))(?<subPath>/[A-Za-z0-9_.\\-]+)*$")),
new VcsRepositoryProvider(git, "git.apache.org/", "Git at Apache", compile("^(?<root>git.apache.org/[a-z0-9_.\\-]+\\.git)(?<subPath>/[A-Za-z0-9_.\\-]+)*$")),
new VcsRepositoryProvider(git, "git.openstack.org/", "OpenStack Git Repository", compile("^(?<root>git\\.openstack\\.org/[A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+)(\\.git)?(?<subPath>/[A-Za-z0-9_.\\-]+)*$")),
new VcsRepositoryProvider(git, "hub.jazz.net/git/", "IBM Bluemix DevOps Services", compile("^(?<root>hub\\.jazz\\.net/git/[a-z0-9]+/[A-Za-z0-9_.\\-]+)(?<subPath>/[A-Za-z0-9_.\\-]+)*$"))
));
private static final List<org.echocat.gradle.plugins.golang.vcs.VcsRepositoryProvider> DEFAULT_CONCRETES = toConcrete(DEFAULTS);

@Nonnull
public static List<VcsRepositoryProvider> defaults() {
return DEFAULTS;
}

@Nonnull
public static List<org.echocat.gradle.plugins.golang.vcs.VcsRepositoryProvider> defaultConcretes() {
return DEFAULT_CONCRETES;
}

@Nonnull
private final String _prefix;
@Nonnull
private final VcsType _type;
@Nonnull
private final String _name;
@Nonnull
private final Pattern _dependencyPattern;

public VcsRepositoryProvider(@Nonnull VcsType type, @Nonnull String prefix, @Nonnull String name, @Nonnull Pattern dependencyPattern) {
_prefix = prefix;
_type = type;
_name = name;
_dependencyPattern = dependencyPattern;
}

@Nonnull
public String getPrefix() {
return _prefix;
}

@Nonnull
public VcsType getType() {
return _type;
}

@Nonnull
public String getName() {
return _name;
}

@Nonnull
public Pattern getDependencyPattern() {
return _dependencyPattern;
}

@Override
public String toString() {
return getName() + "{prefix: " + getPrefix() + ", type: " + getType() + ", dependencyPattern: " + getDependencyPattern() + "}";
}

@Nonnull
public org.echocat.gradle.plugins.golang.vcs.VcsRepositoryProvider toConcrete() {
return new DefaultVcsRepositoryProvider(getType(), getPrefix(), getName(), getDependencyPattern());
}

@Nonnull
public static List<org.echocat.gradle.plugins.golang.vcs.VcsRepositoryProvider> toConcrete(@Nullable Iterable<VcsRepositoryProvider> models) {
final List<org.echocat.gradle.plugins.golang.vcs.VcsRepositoryProvider> result = new ArrayList<>();
if (models != null) {
for (final VcsRepositoryProvider model : models) {
result.add(model.toConcrete());
}
}
return unmodifiableList(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
deleteIfExists(file);
} catch (final IOException e) {
LOGGER.warn("Could not delete file {}.", file, e);
LOGGER.warn("Could not delete file '{}': {}", file, e.toString());
}
return CONTINUE;
}
Expand All @@ -63,7 +63,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
try {
deleteIfExists(dir);
} catch (final IOException e) {
LOGGER.warn("Could not delete dir {}.", dir, e);
LOGGER.warn("Could not delete dir '{}': {}.", dir, e.toString());
}
return SKIP_SUBTREE;
}
Expand All @@ -72,10 +72,12 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
try {
deleteIfExists(dir);
} catch (final IOException e) {
LOGGER.warn("Could not delete dir {}.", dir, e);
if (exc == null) {
try {
deleteIfExists(dir);
} catch (final IOException e) {
LOGGER.warn("Could not delete dir '{}': {}.", dir, e.toString());
}
}
return CONTINUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void run() throws Exception {
protected void prepareDependencyCacheIfNeeded(Path packagePath, Path dependencyCachePath) throws IOException {
final Path packageVendorPath = packagePath.resolve("vendor");

createDirectories(dependencyCachePath);
if (exists(packageVendorPath)) {
if (!isDirectory(packageVendorPath) && !isSymbolicLink(packageVendorPath)) {
throw new IllegalStateException(packageVendorPath + " already exists but is not a symbolic link.");
Expand Down
Loading

0 comments on commit 011680f

Please sign in to comment.