Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit 32c4b13

Browse files
authored
Merge pull request #60 from Inedo/pack-single-file
Closes #44
2 parents d926e58 + 80d8307 commit 32c4b13

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/dotnet/upack/Pack.cs

+22-12
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public sealed class Pack : Command
1919
public string Manifest { get; set; }
2020

2121
[DisplayName("source")]
22-
[Description("Directory containing files to add to the package.")]
22+
[Description("File or directory containing files to add to the package.")]
2323
[PositionalArgument(0)]
2424
[ExpandPath]
25-
public string SourceDirectory { get; set; }
25+
public string SourcePath { get; set; }
2626

2727
[DisplayName("targetDirectory")]
2828
[Description("Directory where the .upack file will be created. If not specified, the current working directory is used.")]
@@ -126,30 +126,40 @@ public override async Task<int> RunAsync(CancellationToken cancellationToken)
126126
info["createdBy"] = Environment.UserName;
127127
}
128128

129-
if (!Directory.Exists(this.SourceDirectory))
129+
if (!Directory.Exists(this.SourcePath) && !File.Exists(this.SourcePath))
130130
{
131-
Console.Error.WriteLine($"The source directory '{this.SourceDirectory}' does not exist.");
131+
Console.Error.WriteLine($"The source directory '{this.SourcePath}' does not exist.");
132132
return 2;
133133
}
134134

135135
string relativePackageFileName = $"{info.Name}-{info.Version.Major}.{info.Version.Minor}.{info.Version.Patch}.upack";
136136
string targetFileName = Path.Combine(this.TargetDirectory ?? Environment.CurrentDirectory, relativePackageFileName);
137137

138-
if (File.Exists(Path.Combine(this.SourceDirectory, relativePackageFileName)))
138+
if (File.Exists(Path.Combine(this.SourcePath, relativePackageFileName)))
139139
{
140140
Console.Error.WriteLine("Warning: output file already exists in source directory and may be included inadvertently in the package contents.");
141141
}
142142

143143
string tmpPath = Path.GetTempFileName();
144144
using (var builder = new UniversalPackageBuilder(tmpPath, info))
145145
{
146-
await builder.AddContentsAsync(
147-
this.SourceDirectory,
148-
"/",
149-
true,
150-
s => string.IsNullOrWhiteSpace(this.Manifest) || !string.Equals(s, "upack.json", StringComparison.OrdinalIgnoreCase),
151-
cancellationToken
152-
);
146+
if (Directory.Exists(this.SourcePath))
147+
{
148+
await builder.AddContentsAsync(
149+
this.SourcePath,
150+
"/",
151+
true,
152+
s => string.IsNullOrWhiteSpace(this.Manifest) || !string.Equals(s, "upack.json", StringComparison.OrdinalIgnoreCase),
153+
cancellationToken
154+
);
155+
}
156+
else
157+
{
158+
using (var file = File.Open(this.SourcePath, FileMode.Open, FileAccess.Read, FileShare.Read))
159+
{
160+
await builder.AddFileAsync(file, Path.GetFileName(this.SourcePath), File.GetLastWriteTimeUtc(this.SourcePath), cancellationToken);
161+
}
162+
}
153163
}
154164

155165
Directory.CreateDirectory(Path.GetDirectoryName(targetFileName));

0 commit comments

Comments
 (0)