Skip to content

Instantly share code, notes, and snippets.

@MrAntix
Last active December 22, 2017 12:52
Show Gist options
  • Save MrAntix/88a19803318fe8a5f1edd52fc4e6d72f to your computer and use it in GitHub Desktop.
Save MrAntix/88a19803318fe8a5f1edd52fc4e6d72f to your computer and use it in GitHub Desktop.
DotNetCore - Update project files with passed version and pack
internal class Program
{
static readonly Regex VersionRE = new Regex(@"(\d+\.\d+\.\d+)(\-.*(\-\d+))?");
static void Main(string[] args)
{
var app = new CommandLineApplication();
var pack = app.Command("pack", config =>
{
var versionArg = config.Argument("version", "Semamtic Version to pack e.g. (1.0.0-beta-1)", false);
config.OnExecute(() =>
{
Match versionMatch;
if (string.IsNullOrWhiteSpace(versionArg.Value)
|| (versionMatch = VersionRE.Match(versionArg.Value)) == null)
{
Console.WriteLine($"Version is required!");
config.ShowHelp();
return 1;
}
Console.WriteLine($"Packing version {versionMatch.Groups[1].Value}{versionMatch.Groups[2].Value}");
UpdateVersionNumbers(versionMatch.Groups[1].Value, versionMatch.Groups[2].Value);
return 0;
});
config.HelpOption("-? | -h | --help"); //show help on --help
});
//give people help with --help
app.HelpOption("-? | -h | --help");
var result = app.Execute(args);
Environment.Exit(result);
}
static void UpdateVersionNumbers(string version, string prereleaseVersion)
{
var now = DateTime.Now;
var revision = (int) (now - new DateTime(now.Year, now.Month, 1)).TotalMinutes;
var updatedFilePaths = Directory
.EnumerateFiles(Environment.CurrentDirectory, "*.csproj", SearchOption.AllDirectories)
.Where(fp => UpdateVersionNumber(fp, version, revision, prereleaseVersion));
foreach (var filePath in updatedFilePaths)
{
Pack(filePath);
Console.WriteLine($"Updated and packed {filePath}");
}
}
static bool UpdateVersionNumber(string filePath, string version, int revision, string prereleaseVersion)
{
try
{
var xml = XDocument.Load(filePath);
if (xml.Root == null) return false;
var copyPackage = xml.Root
.Elements()
.SingleOrDefault(e => (e.Name == "Target")
& e.Attributes().Any(a => a.Value == "CopyPackage"));
if (copyPackage != null)
{
var propertyGroup = xml.Root
.Elements()
.Single(e => e.Name == "PropertyGroup" && e.Elements().Any(se => se.Name == "TargetFramework"));
SetElement(propertyGroup, "Version", $"{version}{prereleaseVersion}");
SetElement(propertyGroup, "AssemblyVersion", $"{version}.{revision}");
SetElement(propertyGroup, "FileVersion", $"{version}.{revision}");
xml.Save(filePath);
return true;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error updating version for {filePath}\n{ex}");
}
return false;
}
static bool Pack(string filePath)
{
var startInfo = new ProcessStartInfo
{
CreateNoWindow = false,
FileName = "dotnet",
Arguments = $"pack {filePath}",
WorkingDirectory = Environment.CurrentDirectory,
RedirectStandardOutput = false
};
var process = Process.Start(startInfo);
process.WaitForExit();
return process.ExitCode == 0;
}
static void SetElement(XContainer container, string name, string value)
{
var element = container.Elements()
.SingleOrDefault(e => e.Name == name);
if (element == null)
{
element = new XElement(name);
container.Add(element);
}
element.Value = value;
}
}
@MrAntix
Copy link
Author

MrAntix commented Dec 22, 2017

create a console app

install-package Microsoft.Extensions.CommandLineUtils

add following to the project files you want packed

  <Target Name="CopyPackage" AfterTargets="Pack">
    <Copy SourceFiles="$(OutputPath)..\$(PackageId).$(PackageVersion).nupkg" DestinationFolder="..\..\..\nuget\$(PackageId)\" />
  </Target>

choose your own DestinationFolder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment