Skip to content

Instantly share code, notes, and snippets.

@freekh
Created December 20, 2013 19:22
Show Gist options
  • Save freekh/8059973 to your computer and use it in GitHub Desktop.
Save freekh/8059973 to your computer and use it in GitHub Desktop.
Example code, resolve a project, then publish a new one:
val standardProgressIndicator = new adept.progress.default.AllEncompassingProgress(System.out)
// the repo we will publish to
val projectRepo = if (GitRepository.exists(Adept.HOME_DIR, projectName)) GitRepository.open(Adept.HOME_DIR, projectName) else GitRepository.init(Adept.HOME_DIR, projectName)
val repo1: RepositoryRef = GitRepository.init(Adept.HOME_DIR, "one") //init new adept repository in HOME_DIR/one
val repo2: RepositoryRef = GitRepository.open(Adept.HOME_DIR, "two") //open adept repository in HOME_DIR/two at HEAD
val repo3: RepositoryRef = GitRepository.clone(Adept.HOME_DIR, "three", "git://server/project.git", standardProgressIndicator) //clone remote to HOME_DIR/three from git using the standard progress indicator
// Can also do: repo3.isFailure() //returns true if repo3 failed to init/open/clone
// And: repo1.addRemote("git://server2/project.git") //adds a new remote just for fun
val adept: Adept = new Adept(repo1, repo2, repo3) //a single instance that manages all repositories
.withLogger(new adept.logging.default.Logger(level = Logger.WARN)) //using a logger
val resolver =
if (adept.isFailure()) throw new Exception(adept.getFailures().mkString(" ")) //Checks if all repositories are open
else
adept.initResolver( //init Resolver instance or throws exception if repository could not be initialized, opened, or cloned
RepositoryHash.fromFile(new File(".repositories")) + new RespositoryHash("13124141b", repo1) + RepositoryHash.head(repo2) //sets the current commit hashes that will be used for resolution
)
//actual resolution, uses internal caching to load files etc... this cache is safe because we know exactly the repositories and requirements that were set
val result: ResolveResult = resolver.resolve(Set(Requirement(Id("A"), Set(Constraint("binary-version", Set("1")), ConfigurationId("compile")), scanPaths = false, standardProgressIndicator)
println(result) //prints out result with graph etc etc useful for debugging
val projectName = "myproject"
if (result.isResolved) { //everything looks good so far, continuing
val adfFile = new File("artifacts.adf")
val currentArtifactDescriptors = Some(adept.getArtifactDecriptors(result, configurations)) //get the artifact descriptors (hashes and locations, used for downloads)
val previousArtifactDescriptors: Option[Seq[ArtifactDescriptor]] = adept.getArtifactDescriptors(adfFile)
//if not exisiting or not equal write adf file...
val artifactDescriptors: Seq[ArtifactDescriptor] =
if (currentArtifactDescriptors != previousArtifactDescriptors) {
adept.writeArtifactDescriptorFile(currentArtifactDescriptors, adfFile).right.get //just throw error if failure to make example easier to follow, Right is the written artifactDescriptors
} else {
currentArtifactDescriptors
}
//then download everything that is not there...
val cachedFiles = {
//download the necessary files, verify the artifacts if not, and return files
val downloadResult: Either[Seq[DownloadFailure], Seq[Files]] = adept.download(artifactDescriptors, overwrite = false, verify = true)
downloadResult
}
val classpath = cachedFiles.mkString(":")
//END TODO
//****
//COMPILE USING classpath...
//****
//add new variants for this project to the local git repo
val requiredRepos: Map[RepositoryRef, Commit] = Adept.requiredRepositories(result) //throws an exception if result failed ....
//create variant
val configurations: Set[Configuration] = Configurations.create(
"compile" -> Set.empty,
"runtime" -> Set("compile")
)
val variant = Variant(Id("mymodule"), Set(Attribute("binary-version", binaryVersions)), configurations, ...)
//add variant to index
adept.add(variant, Configuration(ConfigurationId("compile"), Set.empty), requiredRepos, projectRepo)
//commit the changes
val releaseNotes = new ReleaseNotes(message = "This release is much better than the previous...", metadata = Map("git-commit" -> currentGitCommit, "git-repository" -> "git://github.comg/user/project.git"))a
adept.commit(releaseNotes, projectRepo) //commit index of repo
adept.push(projectRepo) //Push the metadata to remote repository
println("SUCCESS!")
} else {
println(ErrorMessages.result(result, projectName, ....)) //alert user
}
//*************
//Can also do:
//Clean caches and garbage collect:
adept.rmVariantCache() //removes the variants saved in the internal adept cache
adept.rmArtifactCache() //c
repo1.gc(standardProgressIndicator) //meta-data
adept.gcArtifactCache(standardProgressIndicator)
//Shutdown gracefully (writes cache to disk)
adept.shutdown() //shutdown cache manager, clean up git locks?, ...
//Start a server (TBD):
adept.server("http://www1.adepthub.com").serveCache(true).start(standardProgressIndicator)
//iterate through ALL commits in all repositories for each Artifact
//serve meta-data and cached elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment