Exemplo n.º 1
0
        public static async Task RunAsync(UniversalPackage package, string script, bool simulate)
        {
            PackageId      = new UniversalPackageId(package.Group, package.Name);
            PackageVersion = package.Version;

            await ExtensionsManager.WaitForInitializationAsync().ConfigureAwait(false);

            var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));

            try
            {
                Directory.CreateDirectory(tempPath);
                Console.WriteLine("Extracting package...");
                await package.ExtractAllItemsAsync(tempPath, default);

                var installScriptFileName = Path.Combine(tempPath, script);
                var installScript         = Compile(installScriptFileName);
                if (installScript == null)
                {
                    throw new RompException($"Unable to compile {script}.");
                }

                PackageRootPath = tempPath;
                await RunPlanAsync(installScript, simulate);
            }
            finally
            {
                Console.WriteLine("Cleaning up...");
                try { Directory.Delete(tempPath, true); }
                catch { }
            }
        }
Exemplo n.º 2
0
        private async void File_Open(object sender, ExecutedRoutedEventArgs e)
        {
            if (!this.PromptUnsaved())
            {
                return;
            }

            var dialog = new OpenFileDialog
            {
                ValidateNames   = true,
                ShowReadOnly    = true,
                CheckFileExists = true,
                CheckPathExists = true,
                Filter          = "UPack files (*.upack)|*.upack|All files (*.*)|*.*"
            };

            if (dialog.ShowDialog(this) == true)
            {
                using (var file = dialog.OpenFile())
                {
                    this.AddRecentFile(dialog.FileName);
                    if (dialog.ReadOnlyChecked)
                    {
                        this.Package = await UniversalPackage.CreateAsync(file);
                    }
                    else
                    {
                        this.Package = await UniversalPackage.CreateAsync(dialog.FileName, file);
                    }
                }
            }
        }
        protected override async Task <object> RemoteExecuteAsync(IRemoteOperationExecutionContext context)
        {
            var client = new ProGetClient(this.Server, this.FeedName, this.UserName, this.Password, this, context.CancellationToken);

            try
            {
                this.LogInformation($"Pushing package {this.Name} to ProGet...");

                string path = context.ResolvePath(this.FilePath);

                this.LogDebug("Using package file: " + path);

                if (!FileEx.Exists(path))
                {
                    this.LogError(this.FilePath + " does not exist.");
                    return(null);
                }

                if (string.IsNullOrWhiteSpace(this.Name) || string.IsNullOrWhiteSpace(this.Version))
                {
                    try
                    {
                        using (var package = new UniversalPackage(path))
                        {
                            if (string.IsNullOrWhiteSpace(package.Name) || package.Version == null)
                            {
                                this.LogError("Name and Version properties are required unless pushing a package that already has those properties set.");
                                return(null);
                            }
                        }
                    }
                    catch
                    {
                        this.LogError("Name and Version properties are required unless pushing a package that already has those properties set.");
                        return(null);
                    }
                }

                using (var file = FileEx.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var data = new ProGetPackagePushData
                    {
                        Title        = this.Title,
                        Description  = this.Description,
                        Icon         = this.Icon,
                        Dependencies = this.Dependencies?.ToArray()
                    };

                    await client.PushPackageAsync(this.Group, this.Name, this.Version, data, file);
                }
            }
            catch (ProGetException ex)
            {
                this.LogError(ex.FullMessage);
                return(null);
            }

            this.LogInformation("Package pushed.");
            return(null);
        }
Exemplo n.º 4
0
        public override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            using (var packageStream = new FileStream(this.Package, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous))
            {
                UniversalPackageMetadata info;

                try
                {
                    using (var package = new UniversalPackage(packageStream, true))
                    {
                        info = package.GetFullMetadata();
                    }
                }
                catch (Exception ex)
                {
                    throw new UpackException("The specified file is not a valid universal package: " + ex.Message, ex);
                }

                var error = ValidateManifest(info);
                if (error != null)
                {
                    Console.Error.WriteLine("Invalid upack.json: {0}", error);
                    return(2);
                }

                packageStream.Position = 0;

                var client = CreateClient(this.Target, this.Authentication);

                PrintManifest(info);

                try
                {
                    await client.UploadPackageAsync(packageStream, cancellationToken);
                }
                catch (WebException ex)
                {
                    throw ConvertWebException(ex);
                }

                if (!string.IsNullOrEmpty(info.Group))
                {
                    Console.WriteLine($"{info.Group}:{info.Name} {info.Version} published!");
                }
                else
                {
                    Console.WriteLine($"{info.Name} {info.Version} published!");
                }
            }

            return(0);
        }
Exemplo n.º 5
0
        private async void File_OpenRecentFile(object sender, ExecutedRoutedEventArgs e)
        {
            if (!this.PromptUnsaved())
            {
                return;
            }

            var path = (string)e.Parameter;

            this.Package = await UniversalPackage.CreateAsync(path);

            this.AddRecentFile(path);
        }
Exemplo n.º 6
0
 internal static UniversalPackageMetadata GetPackageMetadata(string zipFileName)
 {
     try
     {
         using (var package = new UniversalPackage(zipFileName))
         {
             return(package.GetFullMetadata().Clone());
         }
     }
     catch (Exception ex)
     {
         throw new UpackException($"The source package file '{zipFileName}' does not exist or could not be opened.", ex);
     }
 }
Exemplo n.º 7
0
        private static void Validate(ArgList args)
        {
            var packageName = args.PopCommand();

            if (string.IsNullOrEmpty(packageName))
            {
                throw new RompException("Usage: romp validate <package-file>");
            }

            using (var package = new UniversalPackage(packageName))
            {
                var packageInfo = RompPackInfo.Load(package);
                if (packageInfo.WriteScriptErrors())
                {
                    throw new RompException("Error compiling install script.");
                }

                Console.WriteLine($"Package {new UniversalPackageId(package.Group, package.Name)} validated.");
            }
        }
Exemplo n.º 8
0
        public override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            UniversalPackage package;

            try
            {
                package = new UniversalPackage(this.Package);
            }
            catch (Exception ex)
            {
                throw new UpackException("The specified file is not a valid universal package: " + ex.Message, ex);
            }

            using (package)
            {
                var info = package.GetFullMetadata();
                PrintManifest(info);

                await UnpackZipAsync(this.Target, this.Overwrite, package, this.PreserveTimestamps, cancellationToken);
            }

            return(0);
        }
Exemplo n.º 9
0
        public override async Task ExecuteAsync(IOperationExecutionContext context)
        {
            if (string.IsNullOrWhiteSpace(this.PackageFile))
            {
                if (string.IsNullOrWhiteSpace(this.FeedUrl))
                {
                    this.LogError("FeedUrl is required if PackageFile is not specified.");
                    return;
                }

                if (string.IsNullOrWhiteSpace(this.PackageName))
                {
                    this.LogError("Name is required if PackageFile is not specified.");
                    return;
                }

                var endpoint = new UniversalFeedEndpoint(new Uri(this.FeedUrl), this.UserName, this.Password);

                this.LogInformation($"Getting package information for {this.PackageName} from {endpoint}...");
                var client   = new UniversalFeedClient(endpoint);
                var versions = await client.ListPackageVersionsAsync(UniversalPackageId.Parse(this.PackageName));

                this.LogDebug($"Server return info for {versions.Count} packages.");

                RemoteUniversalPackageVersion package;
                if (!string.IsNullOrWhiteSpace(this.PackageVersion))
                {
                    this.LogDebug($"Checking for {this.PackageVersion} in result set...");
                    var parsedVersion = UniversalPackageVersion.Parse(this.PackageVersion);
                    package = versions.FirstOrDefault(p => p.Version == parsedVersion);
                    if (package != null)
                    {
                        this.LogInformation($"Package {this.PackageName} {this.PackageVersion} found.");
                    }
                    else
                    {
                        this.LogInformation($"Package {this.PackageName} {this.PackageVersion} not found.");
                    }
                }
                else
                {
                    if (versions.Count > 0)
                    {
                        this.LogDebug($"Determining latest version of {this.PackageName}...");
                        package = versions.Aggregate((p1, p2) => p1.Version >= p2.Version ? p1 : p2);
                        this.LogInformation($"Latest version of {this.PackageName} is {package.Version}.");
                    }
                    else
                    {
                        this.LogInformation($"Package {this.PackageName} not found.");
                        package = null;
                    }
                }

                if (package != null)
                {
                    this.Exists   = true;
                    this.Metadata = this.Convert(package.AllProperties).AsDictionary();
                }
                else
                {
                    this.Exists = false;
                }
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(this.FeedUrl))
                {
                    this.LogWarning("FeedUrl is ignored when PackageFile is specified.");
                }

                if (!string.IsNullOrWhiteSpace(this.PackageName))
                {
                    this.LogError("Name is ignored when PackageFile is specified.");
                }

                if (!string.IsNullOrWhiteSpace(this.PackageVersion))
                {
                    this.LogError("Version is ignored when PackageFile is specified.");
                }

                var fileOps = await context.Agent.GetServiceAsync <IFileOperationsExecuter>();

                var fullPath = context.ResolvePath(this.PackageFile);

                this.LogInformation($"Reading {fullPath}...");

                if (await fileOps.FileExistsAsync(fullPath))
                {
                    this.LogDebug("Package file exists; reading metadata...");
                    UniversalPackageMetadata metadata;
                    try
                    {
                        using (var stream = await fileOps.OpenFileAsync(fullPath, FileMode.Open, FileAccess.Read))
                            using (var package = new UniversalPackage(stream))
                            {
                                metadata = package.GetFullMetadata();
                            }
                    }
                    catch (Exception ex)
                    {
                        this.LogError("Error reading package: " + ex);
                        return;
                    }

                    this.Exists   = true;
                    this.Metadata = this.Convert(metadata).AsDictionary();
                }
                else
                {
                    this.LogInformation("Package file not found.");
                    this.Exists = false;
                }
            }
        }
Exemplo n.º 10
0
        public override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            var targetDirectory = this.TargetDirectory;

            if (String.IsNullOrEmpty(targetDirectory))
            {
                targetDirectory = Environment.CurrentDirectory;
            }

            var client = CreateClient(this.SourceUrl, this.Authentication);
            UniversalPackageId id;

            try
            {
                id = UniversalPackageId.Parse(this.PackageName);
            }
            catch (ArgumentException ex)
            {
                throw new UpackException("Invalid package ID: " + ex.Message, ex);
            }
            var version = await GetVersionAsync(client, id, this.Version, this.Prerelease, cancellationToken);

            Stream stream = null;

            if (!this.Unregistered)
            {
                using (var registry = PackageRegistry.GetRegistry(this.UserRegistry))
                {
                    await registry.LockAsync(cancellationToken);

                    try
                    {
                        if (this.CachePackages)
                        {
                            stream = await registry.TryOpenFromCacheAsync(id, version, cancellationToken);
                        }

                        await registry.RegisterPackageAsync(new RegisteredPackage
                        {
                            FeedUrl            = this.SourceUrl,
                            Group              = id.Group,
                            Name               = id.Name,
                            Version            = version.ToString(),
                            InstallPath        = this.TargetDirectory,
                            InstallationDate   = DateTimeOffset.Now.ToString("o"),
                            InstallationReason = this.Comment,
                            InstalledBy        = Environment.UserName,
                            InstalledUsing     = Assembly.GetEntryAssembly().GetName().Name + "/" + Assembly.GetEntryAssembly().GetName().Version.ToString()
                        });
                    }
                    finally
                    {
                        await registry.UnlockAsync();
                    }
                }
            }

            if (stream == null)
            {
                try
                {
                    stream = await client.GetPackageStreamAsync(id, version, cancellationToken);
                }
                catch (WebException ex)
                {
                    throw ConvertWebException(ex, PackageNotFoundMessage);
                }
            }

            using (var package = new UniversalPackage(stream))
            {
                await UnpackZipAsync(this.TargetDirectory, this.Overwrite, package, this.PreserveTimestamps, cancellationToken);
            }

            return(0);
        }
Exemplo n.º 11
0
#pragma warning disable CS0612 // Type or member is obsolete
        public override async Task <int> RunAsync(CancellationToken cancellationToken)
        {
            if (this.NoAudit && !string.IsNullOrEmpty(this.Note))
            {
                Console.Error.WriteLine("--no-audit cannot be used with --note.");
                return(2);
            }

            var info        = GetPackageMetadata(this.SourcePath);
            var infoToMerge = await GetMetadataToMergeAsync();

            var hash = GetSHA1(this.SourcePath);

            var id = (string.IsNullOrEmpty(info.Group) ? "" : info.Group + "/") + info.Name + ":" + info.Version + ":" + hash;

            foreach (var modifiedProperty in infoToMerge)
            {
                info[modifiedProperty.Key] = modifiedProperty.Value;
            }

            var error = ValidateManifest(info);

            if (error != null)
            {
                Console.Error.WriteLine("Invalid {0}: {1}", string.IsNullOrWhiteSpace(this.Manifest) ? "parameters" : "upack.json", error);
                return(2);
            }

            PrintManifest(info);

            if (!this.NoAudit)
            {
                JArray history;
                if (info.ContainsKey("repackageHistory"))
                {
                    history = (JArray)info["repackageHistory"];
                }
                else
                {
                    history = new JArray();
                    info["repackageHistory"] = history;
                }

                var entry = new Dictionary <string, object>
                {
                    { "id", id },
                    { "date", DateTime.UtcNow.ToString("u") },
                    { "using", "upack/" + typeof(Repack).Assembly.GetName().Version.ToString(3) },
                    { "by", Environment.UserName }
                };

                if (!string.IsNullOrEmpty(this.Note))
                {
                    entry["reason"] = this.Note;
                }

                history.Add(JObject.FromObject(entry));
            }

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

            if (!this.Overwrite && File.Exists(targetFileName))
            {
                throw new UpackException($"Target file '{targetFileName}' exists and overwrite was set to false.");
            }

            string tmpPath = Path.GetTempFileName();

            using (var existingPackage = new UniversalPackage(this.SourcePath))
                using (var builder = new UniversalPackageBuilder(tmpPath, info))
                {
                    var entries = from e in existingPackage.Entries
                                  where !string.Equals(e.RawPath, "upack.json", StringComparison.OrdinalIgnoreCase)
                                  select e;

                    foreach (var entry in entries)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        if (entry.IsDirectory)
                        {
                            builder.AddEmptyDirectoryRaw(entry.RawPath);
                        }
                        else
                        {
                            using (var stream = entry.Open())
                            {
                                await builder.AddFileRawAsync(stream, entry.RawPath, entry.Timestamp, cancellationToken);
                            }
                        }
                    }
                }

            Directory.CreateDirectory(Path.GetDirectoryName(targetFileName));
            File.Delete(targetFileName);
            File.Move(tmpPath, targetFileName);

            return(0);
        }
Exemplo n.º 12
0
        internal static async Task UnpackZipAsync(string targetDirectory, bool overwrite, UniversalPackage package, bool preserveTimestamps, CancellationToken cancellationToken)
        {
            Directory.CreateDirectory(targetDirectory);

            var entries = package.Entries.Where(e => e.IsContent);

            int files       = 0;
            int directories = 0;

            foreach (var entry in entries)
            {
                var targetPath = Path.Combine(targetDirectory, entry.ContentPath);

                if (entry.IsDirectory)
                {
                    Directory.CreateDirectory(targetPath);
                    directories++;
                }
                else
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
                    using (var entryStream = entry.Open())
                        using (var targetStream = new FileStream(targetPath, overwrite ? FileMode.Create : FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous))
                        {
                            await entryStream.CopyToAsync(targetStream, 65536, cancellationToken);
                        }

                    // Assume files with timestamps set to 0 (DOS time) or close to 0 are not timestamped.
                    if (preserveTimestamps && entry.Timestamp.Year > 1980)
                    {
                        File.SetLastWriteTimeUtc(targetPath, entry.Timestamp.DateTime);
                    }

                    files++;
                }
            }

            Console.WriteLine($"Extracted {files} files and {directories} directories.");
        }
Exemplo n.º 13
0
        protected override async Task <object> RemoteExecuteAsync(IRemoteOperationExecutionContext context)
        {
            var fullPath = context.ResolvePath(this.FileName);

            this.LogInformation($"Changing \"{fullPath}\" package version to {AH.CoalesceString(this.NewVersion, "remove pre-release label")}...");

            var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("n"));

            try
            {
                DirectoryEx.Create(tempPath);
                UniversalPackageMetadata currentMetadata;
                using (var upack = new UniversalPackage(fullPath))
                {
                    currentMetadata = upack.GetFullMetadata();
                    await upack.ExtractAllItemsAsync(tempPath, context.CancellationToken);

                    FileEx.Delete(PathEx.Combine(tempPath, "upack.json"));
                }

                var newMetadata = currentMetadata.Clone();
                if (string.IsNullOrEmpty(this.NewVersion))
                {
                    newMetadata.Version = new UniversalPackageVersion(currentMetadata.Version.Major, currentMetadata.Version.Minor, currentMetadata.Version.Patch);
                }
                else
                {
                    newMetadata.Version = UniversalPackageVersion.Parse(this.NewVersion);
                }

                if (currentMetadata.Version == newMetadata.Version)
                {
                    this.LogWarning($"Current package version {currentMetadata.Version} and the new version {newMetadata.Version} are the same; nothing to do.");
                    return(null);
                }

                this.LogInformation("New version: " + newMetadata.Version);

                this.LogDebug("Adding repacking entry...");
                newMetadata.RepackageHistory.Add(
                    new RepackageHistoryEntry
                {
                    Id     = new UniversalPackageId(currentMetadata.Group, currentMetadata.Name) + ":" + currentMetadata.Version,
                    Date   = DateTimeOffset.Now,
                    Using  = SDK.ProductName + "/" + SDK.ProductVersion,
                    Reason = this.Reason
                }
                    );

                using (var builder = new UniversalPackageBuilder(fullPath, newMetadata))
                {
                    await builder.AddRawContentsAsync(tempPath, string.Empty, true, c => true, context.CancellationToken);
                }

                this.LogInformation("Package version changed.");

                return(null);
            }
            finally
            {
                try
                {
                    this.LogDebug($"Deleting temporary files from {tempPath}...");
                    DirectoryEx.Clear(tempPath);
                    DirectoryEx.Delete(tempPath);
                }
                catch (Exception ex)
                {
                    this.LogWarning("Unable to delete temporary files: " + ex.Message);
                }
            }
        }
Exemplo n.º 14
0
        public static RompPackInfo Load(UniversalPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }

            {
                var packageVariables = new Dictionary <string, PackageVariable>(StringComparer.OrdinalIgnoreCase);

                var varEntry = package.GetRawEntry("packageVariables.json");
                if (varEntry != null)
                {
                    try
                    {
                        JObject varObj;

                        using (var varStream = varEntry.Value.Open())
                            using (var streamReader = new StreamReader(varStream, InedoLib.UTF8Encoding))
                                using (var jsonReader = new JsonTextReader(streamReader))
                                {
                                    varObj = JObject.Load(jsonReader);
                                }

                        foreach (var prop in varObj.Properties())
                        {
                            packageVariables[prop.Name] = new PackageVariable(prop.Value);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new RompException("Invalid packageVariables.json file.", ex);
                    }
                }

                if (!packageVariables.ContainsKey("TargetDirectory"))
                {
                    packageVariables.Add("TargetDirectory", PackageVariable.DefaultTargetDirectoryVariable);
                }

                var packageCredentials = new Dictionary <string, PackageCredentials>(StringComparer.OrdinalIgnoreCase);

                var credsEntry = package.GetRawEntry("packageCredentials.json");
                if (credsEntry != null)
                {
                    try
                    {
                        JArray credsArray;

                        using (var credsStream = credsEntry.Value.Open())
                            using (var streamReader = new StreamReader(credsStream, InedoLib.UTF8Encoding))
                                using (var jsonReader = new JsonTextReader(streamReader))
                                {
                                    credsArray = JArray.Load(jsonReader);
                                }

                        foreach (var token in credsArray)
                        {
                            if (!(token is JObject obj))
                            {
                                throw new RompException("Invalid token in packageCredentials.json file.");
                            }

                            var creds = new PackageCredentials(obj);
                            packageCredentials[creds.Type + "::" + creds.Name] = creds;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new RompException("Invalid packageCredentials.json file.", ex);
                    }
                }

                string installScript      = null;
                var    installScriptEntry = package.GetRawEntry("install.otter");
                if (installScriptEntry != null)
                {
                    using (var installScriptStream = installScriptEntry.Value.Open())
                        using (var streamReader = new StreamReader(installScriptStream, InedoLib.UTF8Encoding))
                        {
                            installScript = streamReader.ReadToEnd();
                        }
                }

                return(new RompPackInfo(packageVariables, packageCredentials, installScript));
            }
        }