Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit ed4b4ed

Browse files
trurl123kunga
authored andcommitted
add prerelease option for 'cm pack' command (#47)
* add prerelease option for 'cm pack' command * set error level on 'cm pack' error
1 parent 62a8146 commit ed4b4ed

File tree

7 files changed

+20
-12
lines changed

7 files changed

+20
-12
lines changed

Commands/PackCommand.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Common;
23
using Common.YamlParsers;
34
using System.IO;
@@ -10,6 +11,7 @@ public class PackCommand : Command
1011
private string project;
1112
private string configuration;
1213
private BuildSettings buildSettings;
14+
private bool preRelease = false;
1315

1416
public PackCommand() : base(new CommandSettings
1517
{
@@ -35,7 +37,7 @@ protected override int Execute()
3537
var csproj = new ProjectFile(projectPath);
3638
var deps = new DepsParser(modulePath).Get(configuration);
3739
ConsoleWriter.WriteInfo("patching csproj");
38-
var patchedDocument = csproj.CreateCsProjWithNugetReferences(deps.Deps, modulePath);
40+
var patchedDocument = csproj.CreateCsProjWithNugetReferences(deps.Deps, preRelease);
3941
var backupFileName = Path.Combine(Path.GetDirectoryName(projectPath) ?? "", "backup." + Path.GetFileName(projectPath));
4042
if (File.Exists(backupFileName))
4143
File.Delete(backupFileName);
@@ -46,7 +48,8 @@ protected override int Execute()
4648
var moduleBuilder = new ModuleBuilder(Log, buildSettings);
4749
moduleBuilder.Init();
4850
ConsoleWriter.WriteInfo("start pack");
49-
moduleBuilder.DotnetPack(modulePath, projectPath, buildData?.Configuration ?? "Release");
51+
if (!moduleBuilder.DotnetPack(modulePath, projectPath, buildData?.Configuration ?? "Release"))
52+
return -1;
5053
}
5154
finally
5255
{
@@ -64,6 +67,8 @@ protected override void ParseArgs(string[] args)
6467
//dep = new Dep((string)parsedArgs["module"]);
6568
if (parsedArgs["configuration"] != null)
6669
configuration = (string)parsedArgs["configuration"];
70+
preRelease = (bool)parsedArgs["prerelease"];
71+
6772
buildSettings = new BuildSettings
6873
{
6974
ShowAllWarnings = (bool)parsedArgs["warnings"],

Common/ArgumentParser.cs

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public static Dictionary<string, object> ParsePack(string[] args)
9999
{"obsolete", false},
100100
{"verbose", false},
101101
{"progress", false},
102+
{"prerelease", false}
102103
};
103104
var parser = new OptionSet
104105
{
@@ -107,6 +108,7 @@ public static Dictionary<string, object> ParsePack(string[] args)
107108
{"W", f => parsedArguments["obsolete"] = true},
108109
{"v|verbose", v => parsedArguments["verbose"] = true},
109110
{"p|progress", p => parsedArguments["progress"] = true},
111+
{"prerelease", p => parsedArguments["prerelease"] = true},
110112
};
111113
args = parser.Parse(args).ToArray();
112114

Common/ModuleBuilder.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public void Init()
2525
VsDevHelper.ReplaceVariablesToVs();
2626
}
2727

28-
public void DotnetPack(string directory, string projectFileName, string buildConfiguration)
28+
public bool DotnetPack(string directory, string projectFileName, string buildConfiguration)
2929
{
3030
var runner = PrepareShellRunner();
3131
var exitCode = runner.RunInDirectory(directory, $"dotnet pack \\\"{projectFileName}\\\" -c {buildConfiguration}");
3232
ConsoleWriter.Write(runner.Output);
33-
if (exitCode != 0)
34-
{
35-
log.Warn($"Failed to build nuget package {projectFileName}. \nOutput: \n{runner.Output} \nError: \n{runner.Errors} \nExit code: {exitCode}");
36-
}
33+
if (exitCode == 0)
34+
return true;
35+
log.Warn($"Failed to build nuget package {projectFileName}. \nOutput: \n{runner.Output} \nError: \n{runner.Errors} \nExit code: {exitCode}");
36+
return false;
3737
}
3838

3939
public void NugetRestore(Dep dep, string nugetRunCommand)

Common/NuGetHelper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public static class NuGetHelper
88
{
99
private static readonly ILog Log = LogManager.GetLogger(typeof(NuGetHelper));
1010

11-
public static string GetNugetPackageVersion(string packageName, string nugetRunCommand)
11+
public static string GetNugetPackageVersion(string packageName, string nugetRunCommand, bool preRelease)
1212
{
1313
var shellRunner = new ShellRunner();
1414
ConsoleWriter.WriteProgressWithoutSave("Get package verion for " + packageName);
1515

16-
shellRunner.Run($"{nugetRunCommand} list {packageName} -NonInteractive");
16+
shellRunner.Run($"{nugetRunCommand} list {packageName} -NonInteractive" + (preRelease ? " -PreRelease" : ""));
1717
foreach (var line in shellRunner.Output.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries))
1818
{
1919
var lineTokens = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

Common/ProjectFile.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void ReplaceRef(string refName, string refPath)
161161
}
162162
}
163163

164-
public XmlDocument CreateCsProjWithNugetReferences(List<Dep> deps, string moduleDirectory)
164+
public XmlDocument CreateCsProjWithNugetReferences(List<Dep> deps, bool preRelease)
165165
{
166166
if (!newFormat)
167167
throw new Exception("Only new csproj format supported");
@@ -192,7 +192,7 @@ public XmlDocument CreateCsProjWithNugetReferences(List<Dep> deps, string module
192192
var includeAttr = patchedProjDoc.CreateAttribute("Include");
193193
includeAttr.Value = dep.Name;
194194
refElement.Attributes.Append(includeAttr);
195-
var packageVersion = NuGetHelper.GetNugetPackageVersion(dep.Name, nugetRunCommand);
195+
var packageVersion = NuGetHelper.GetNugetPackageVersion(dep.Name, nugetRunCommand, preRelease);
196196
if (!string.IsNullOrEmpty(packageVersion))
197197
{
198198
var versionAttr = patchedProjDoc.CreateAttribute("Version");

README-commands.md

+1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@
334334

335335
-p/--progress - show msbuild output in one line
336336

337+
-prerelease - load prerelease versions of packages
337338

338339
### cm status
339340

Tests/ParsersTests/TestProjectFile.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public void TestMakeCsProjWithNugetReferences()
290290
";
291291

292292
var proj = CreateProjectFile(content);
293-
var xmlDocument = proj.CreateCsProjWithNugetReferences(new List<Dep> { new Dep("vostok.core") }, ".");
293+
var xmlDocument = proj.CreateCsProjWithNugetReferences(new List<Dep> { new Dep("vostok.core") }, true);
294294

295295
Assert.Null(xmlDocument.SelectSingleNode("//Reference[@Include='Vostok.Core']"));
296296
Assert.NotNull(xmlDocument.SelectSingleNode("//PackageReference[@Include='vostok.core']"));

0 commit comments

Comments
 (0)