public void CopyDependencies(
Dictionary<string, Dependency> dependencies,
string destDirectory,
OverwriteConfirmation confirmer)
{
if (!Directory.Exists(destDirectory))
{
Directory.CreateDirectory(destDirectory);
}
foreach (Dependency dep in dependencies.Values)
{
// match artifact-*. The - is important to distinguish art-1.0.0
// from artifact-1.0.0 (or base and basement).
string[] dups = Directory.GetFileSystemEntries(destDirectory,
dep.Artifact + "-*");
bool doCopy = true;
bool doCleanup = false;
foreach (string s in dups)
{
// skip the .meta files (a little Unity creeps in).
if (s.EndsWith(".meta"))
{
continue;
}
// Strip the package extension from filenames. Directories generated from
// unpacked AARs do not have extensions.
string existing = Directory.Exists(s) ? s :
Path.GetFileNameWithoutExtension(s);
// Extract the version from the filename.
// dep.Artifact is the name of the package (prefix)
// The regular expression extracts the version number from the filename
// handling filenames like foo-1.2.3-alpha.
string artifactName = existing.Substring(dep.Artifact.Length + 1);
System.Text.RegularExpressions.Match match =
System.Text.RegularExpressions.Regex.Match(artifactName, "^([0-9.]+)");
if (!match.Success) continue;
string artifactVersion = match.Groups[1].Value;
Dependency oldDep = new Dependency(dep.Group, artifactName, artifactVersion,
packageIds: dep.PackageIds,
repositories: dep.Repositories);
// add the artifact version so BestVersion == version.
oldDep.AddVersion(oldDep.Version);
// If the existing artifact matches the new dependency, don't modify it.
if (dep.Key == oldDep.Key) continue;
doCleanup = doCleanup || confirmer == null || confirmer(oldDep, dep);
if (doCleanup)
{
DeleteExistingFileOrDirectory(s);
}
else
{
doCopy = false;
}
}
if (doCopy)
{
string aarFile = null;
// TODO(wilkinsonclay): get the extension from the pom file.
string baseName = null;
string extension = null;
foreach (string ext in Packaging)
{
string fname = Path.Combine(dep.BestVersionPath,
dep.Artifact + "-" + dep.BestVersion + ext);
if (File.Exists(fname))
{
baseName = dep.Artifact + "-" + dep.BestVersion;
aarFile = fname;
extension = ext;
}
}
if (aarFile != null)
{
string destName = Path.Combine(destDirectory, baseName) +
(extension == ".srcaar" ? ".aar" : extension);
string destNameUnpacked = Path.Combine(
destDirectory, Path.GetFileNameWithoutExtension(destName));
string existingName =
File.Exists(destName) ? destName :
Directory.Exists(destNameUnpacked) ? destNameUnpacked : null;
if (existingName != null)
{
doCopy = File.GetLastWriteTime(existingName).CompareTo(
File.GetLastWriteTime(aarFile)) < 0;
if (doCopy) DeleteExistingFileOrDirectory(existingName);
}
if (doCopy)
{
File.Copy(aarFile, destName);
}
}
else
{
throw new ResolutionException("Cannot find artifact for " +
dep);
}
}
}
}