Skip to content

Instantly share code, notes, and snippets.

@freekh
Created December 19, 2013 22:06
Show Gist options
  • Save freekh/8047053 to your computer and use it in GitHub Desktop.
Save freekh/8047053 to your computer and use it in GitHub Desktop.
Example of Adept API usage: initialises and resolves some dependencies (requirements), downloads the jars needed then publishes
val standardProgressIndicator = new adept.progress.default.AllEncompassingProgress(System.out)
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
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 do it. 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")))), 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 = adept.getArtifactDecriptors(result) //get the artifact descriptors (hashes and locations, used for
val previousArtifactDescriptors = adept.getArtifactDescritorFile(adfFile)
val artifactDownloadResult: Either[Seq[String], Seq[File]] =
if (currentArtifactDescriptors != previousArtifactDescriptors) {
adept.writeArtifactDescriptorFile(adfFile) //returns (jar) files if successful
} else {
adept.cache(currentArtifactDescriptors) //download the artifacts and returns files
}
val classpath = artifactDownloadResult match {
case Left(errorMsgs) => throw new Exception(ErrorMessages.artifact(errorMsgs)) //throw exception if adf file could not be written
case Right(files) => adept.checkCache(result) match {
case Right(files) => files
case Left(errorMsgs) => throw new Exception(ErrorMessages.cacheVerification(errorMsgs)) //throw exception if adf file could not be written
}.mkString(":")
//****
//COMPILE USING classpath...
//****
//PUBLISH:
//1. add new variants for this project to the local git repo
val projectRepo = if (GitRepository.exists(Adept.HOME_DIR, projectName)) GitRepository.open(Adept.HOME_DIR, projectName) else GitRepository.init(Adept.HOME_DIR, projectName)
val requiredRepos: Map[RepositoryRef, Commit] = result.repositories
Adept.add(Variant(Id("mymodule"), Set(Attribute("binary-version", binaryVersions)), ...), Configuration(ConfigurationId("compile"), Set.empty), requiredRepos, projectRepo) //throws an exception if result failed ....
//2. commit the changes
val releaseNotes = new ReleaseNotes(message = "This release is much better than the previous...", metadata = Map("git-commit" -> currentGitCommit, "git-repository" -> "git://git.521000.bestg/user/project.git"))a
adept.commit(releaseNotes) //Creates a new git commit in the repositories that are dirty
//3. Push the metadata to remote repository
adept.push(projectRepo)
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:
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