Browse Source

clean up

pull/69/head
Scott Williams 10 years ago
parent
commit
007461b7c7
  1. 233
      build/Program.cs

233
build/Program.cs

@ -25,122 +25,124 @@ namespace ConsoleApplication
//lets find the repo //lets find the repo
var repo = new LibGit2Sharp.Repository(root); var repo = new LibGit2Sharp.Repository(root);
//lets find all the project.json files in the src folder (don't care about versioning tests) //lets find all the project.json files in the src folder (don't care about versioning `tests`)
var projectFiles = Directory.EnumerateFiles(Path.Combine(root, "src"), Project.FileName, SearchOption.AllDirectories); var projectFiles = Directory.EnumerateFiles(Path.Combine(root, "src"), Project.FileName, SearchOption.AllDirectories);
//open them and convert them to source projects //open them and convert them to source projects
var projects = projectFiles.Select(x => ProjectReader.GetProject(x)) var projects = projectFiles.Select(x => ProjectReader.GetProject(x))
.Select(x => new SourceProject(x, repo.Info.WorkingDirectory)) .Select(x => new SourceProject(x, repo.Info.WorkingDirectory))
.ToList(); .ToList();
if (resetmode) if (resetmode)
{ {
if (File.Exists("build-inner.bak")) ResetProject(projects);
{
File.Copy("build-inner.bak", "build-inner.cmd", true);
File.Delete("build-inner.bak");
}
//revert the project.json change be reverting it but skipp all the git stuff as its not needed
foreach (var p in projects)
{
if (File.Exists($"{p.FullProjectFilePath}.bak"))
{
File.Copy($"{p.FullProjectFilePath}.bak", p.FullProjectFilePath, true);
File.Delete($"{p.FullProjectFilePath}.bak");
}
Console.WriteLine($"{p.Name} {p.Version.ToFullString()}");
}
} }
else else
{ {
// populate the dependency chains CaclulateProjectVersionNumber(projects, repo);
projects.ForEach(x => x.PopulateDependencies(projects));
UpdateVersionNumbers(projects);
CreateBuildScript(projects);
foreach (var p in projects) foreach (var p in projects)
{ {
foreach (var c in repo.Commits) Console.WriteLine($"{p.Name} {p.FinalVersionNumber}");
{
// lets apply each commit to the projects looking to see if they effect it or a
// dependency and detect if the commit was one that sent the current version
if (!p.ApplyCommit(c, repo))
{
// if it did create the version then stop looking this one wins
break;
}
}
} }
}
}
// lets build version friendly commit private static void CreateBuildScript(IEnumerable<SourceProject> projects)
string branch = repo.Head.FriendlyName; {
var sb = new StringBuilder();
foreach (var p in projects)
{
// lets see if we are running in appveyor sb.AppendLine($@"dotnet pack --configuration Release --output ""artifacts\bin\ImageSharp"" ""{p.ProjectFilePath}""");
var appveryorBranch = Environment.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH"); }
if (!string.IsNullOrWhiteSpace(appveryorBranch)) File.Copy("build-inner.cmd", "build-inner.bak", true);
{ File.WriteAllText("build-inner.cmd", sb.ToString());
branch = appveryorBranch; }
}
var prNumber = Environment.GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER"); private static void UpdateVersionNumbers(IEnumerable<SourceProject> projects)
if (!string.IsNullOrWhiteSpace(prNumber)) {
{ foreach (var p in projects)
branch = "PR" + prNumber; {
} //TODO force update of all dependent projects to point to the newest build.
if(branch == "(no branch)")
{
throw new Exception("unable to find branch");
}
branch = branch.Replace("/", "-").Replace("--", "-");
if (branch == "master")
{
branch = "";
}
var sb = new StringBuilder(); //we skip the build number and standard CI prefix on first commits
foreach (var p in projects) var newVersion = p.FinalVersionNumber;
{
//TODO force update of all dependent projects to point to the newest build. // create a backup file so we can rollback later without breaking formatting
File.Copy(p.FullProjectFilePath, $"{p.FullProjectFilePath}.bak", true);
//we skip the build number and standard CI prefix on first commits dynamic projectFile = JsonConvert.DeserializeObject(File.ReadAllText(p.FullProjectFilePath));
var newVersion = p.CalculateVersionNumber(branch);
File.Copy(p.FullProjectFilePath, $"{p.FullProjectFilePath}.bak", true);
dynamic projectFile = JsonConvert.DeserializeObject(File.ReadAllText(p.FullProjectFilePath));
projectFile.version = $"{newVersion}-*"; projectFile.version = $"{newVersion}-*";
File.WriteAllText(p.FullProjectFilePath, JsonConvert.SerializeObject(projectFile, Formatting.Indented)); File.WriteAllText(p.FullProjectFilePath, JsonConvert.SerializeObject(projectFile, Formatting.Indented));
}
Console.WriteLine($"{p.Name} {newVersion}"); }
sb.AppendLine($@"dotnet pack --configuration Release --output ""artifacts\bin\ImageSharp"" ""{p.ProjectFilePath}"""); private static string CurrentBranch(Repository repo)
} {
// lets build version friendly commit
string branch = repo.Head.FriendlyName;
// lets see if we are running in appveyor and if we are use the environment variables instead of the head
var appveryorBranch = Environment.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH");
if (!string.IsNullOrWhiteSpace(appveryorBranch))
{
branch = appveryorBranch;
}
var prNumber = Environment.GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER");
if (!string.IsNullOrWhiteSpace(prNumber))
{
branch = $"PR{int.Parse(prNumber):0000}";
}
File.Copy("build-inner.cmd", "build-inner.bak", true); // this will happen when checking out a comit directly and not a branch (like appveryor does when it builds)
File.WriteAllText("build-inner.cmd", sb.ToString()); if (branch == "(no branch)")
{
throw new Exception("unable to find branch");
} }
// clean branch names (might need to be improved)
branch = branch.Replace("/", "-").Replace("--", "-");
return branch;
} }
private static void CaclulateProjectVersionNumber(List<SourceProject> projects, Repository repo)
{
var branch = CurrentBranch(repo);
//find project root // populate the dependency chains
public static string GetProjectRoot() projects.ForEach(x => x.PopulateDependencies(projects));
// update the final version based on the repo history and the currentr branch name
projects.ForEach(x => x.CalculateVersion(repo, branch));
}
private static void ResetProject(List<SourceProject> projects)
{ {
var path = Path.GetFullPath("."); if (File.Exists("build-inner.bak"))
do
{ {
var jsonPath = Path.Combine(path, "global.json"); File.Copy("build-inner.bak", "build-inner.cmd", true);
if (File.Exists(jsonPath)) File.Delete("build-inner.bak");
{ }
return path;
} //revert the project.json change be reverting it but skipp all the git stuff as its not needed
else foreach (var p in projects)
{
if (File.Exists($"{p.FullProjectFilePath}.bak"))
{ {
path = Path.GetDirectoryName(path); File.Copy($"{p.FullProjectFilePath}.bak", p.FullProjectFilePath, true);
File.Delete($"{p.FullProjectFilePath}.bak");
} }
} while (!string.IsNullOrWhiteSpace(path)); }
return null;
} }
public class SourceProject public class SourceProject
{ {
private readonly IEnumerable<string> dependencies; private readonly IEnumerable<string> dependencies;
@ -153,8 +155,9 @@ namespace ConsoleApplication
public string Name { get; private set; } public string Name { get; private set; }
public string ProjectFilePath { get; private set; } public string ProjectFilePath { get; private set; }
private int commitCount = 0; public int CommitCountSinceVersionChange { get; private set; } = 0;
public string FullProjectFilePath { get; private set; } public string FullProjectFilePath { get; private set; }
public string FinalVersionNumber { get; private set; }
public SourceProject(Project project, string root) public SourceProject(Project project, string root)
{ {
@ -164,6 +167,7 @@ namespace ConsoleApplication
this.FullProjectFilePath = project.ProjectFilePath; this.FullProjectFilePath = project.ProjectFilePath;
this.Version = project.Version; this.Version = project.Version;
this.dependencies = project.Dependencies.Select(x => x.Name); this.dependencies = project.Dependencies.Select(x => x.Name);
this.FinalVersionNumber = Version.ToFullString();
} }
public void PopulateDependencies(IEnumerable<SourceProject> projects) public void PopulateDependencies(IEnumerable<SourceProject> projects)
@ -186,9 +190,9 @@ namespace ConsoleApplication
return false; return false;
} }
internal bool ApplyCommitInternal(Commit commit, TreeChanges changes, Repository repo) private bool ApplyCommitInternal(Commit commit, TreeChanges changes, Repository repo)
{ {
commitCount++; CommitCountSinceVersionChange++;
//return false if this is a version number root //return false if this is a version number root
var projectFileChange = changes.Where(x => x.Path?.Equals(this.ProjectFilePath, StringComparison.OrdinalIgnoreCase) == true).FirstOrDefault(); var projectFileChange = changes.Where(x => x.Path?.Equals(this.ProjectFilePath, StringComparison.OrdinalIgnoreCase) == true).FirstOrDefault();
@ -212,13 +216,30 @@ namespace ConsoleApplication
} }
} }
} }
// version must have been the same lets carry on // version must have been the same lets carry on
return true; return true;
} }
return true; return true;
} }
internal bool ApplyCommit(Commit commit, Repository repo)
internal void CalculateVersion(Repository repo, string branch)
{
foreach(var c in repo.Commits)
{
if(!ApplyCommit(c, repo))
{
//we have finished lets populate the final version number
this.FinalVersionNumber = CalculateVersionNumber(branch);
return;
}
}
}
private bool ApplyCommit(Commit commit, Repository repo)
{ {
foreach (var parent in commit.Parents) foreach (var parent in commit.Parents)
{ {
@ -247,53 +268,63 @@ namespace ConsoleApplication
return true; return true;
} }
internal string CalculateVersionNumber(string branch) private string CalculateVersionNumber(string branch)
{ {
var version = this.Version.ToFullString(); var version = this.Version.ToFullString();
if (this.commitCount == 1 && branch == "") //master only if (this.CommitCountSinceVersionChange == 1 && branch == "master") //master only
{ {
if (this.Version.IsPrerelease) if (this.Version.IsPrerelease)
{ {
//prerelease always needs the build counter just not on a branch name //prerelease always needs the build counter just not on a branch name
return $"{version}-{this.commitCount:00000}"; return $"{version}-{this.CommitCountSinceVersionChange:00000}";
} }
//only 1 commit (the changing one) we will skip appending suffix // this is the full release happy path, first commit after changing the version number
return version; return version;
} }
var rootSpecialVersion = ""; var rootSpecialVersion = "";
if (this.Version.IsPrerelease) if (this.Version.IsPrerelease)
{ {
// probably a much easy way for doing this but it work sell enough for a build script
var parts = version.Split(new[] { '-' }, 2); var parts = version.Split(new[] { '-' }, 2);
version = parts[0]; version = parts[0];
rootSpecialVersion = parts[1]; rootSpecialVersion = parts[1];
} }
if(rootSpecialVersion.Length > 0)
// if master and the version doesn't manually specify a prerelease tag force one on for CI builds
if (branch == "master")
{ {
rootSpecialVersion = "-" + rootSpecialVersion; if (!this.Version.IsPrerelease)
{
branch = fallbackTag;
}else
{
branch = "";
}
} }
if (branch == "" && !this.Version.IsPrerelease)
if (rootSpecialVersion.Length > 0)
{ {
branch = fallbackTag; rootSpecialVersion = "-" + rootSpecialVersion;
} }
if (branch.Length > 0) if (branch.Length > 0)
{ {
branch = "-" + branch; branch = "-" + branch;
} }
var maxLength = 20; var maxLength = 20; // dotnet will fail to populate the package if the tag is > 20
maxLength -= rootSpecialVersion.Length; maxLength -= rootSpecialVersion.Length; // this is a required tag
maxLength -= 7; // for the counter and dashes maxLength -= 7; // for the counter and dashes
if(branch.Length > maxLength) if(branch.Length > maxLength)
{ {
branch = branch.Substring(0, maxLength); branch = branch.Substring(0, maxLength);
} }
return $"{version}{rootSpecialVersion}{branch}-{this.commitCount:00000}"; return $"{version}{rootSpecialVersion}{branch}-{this.CommitCountSinceVersionChange:00000}";
} }
} }
} }

Loading…
Cancel
Save