Example #1
0
        internal static string ValidateManifest(UniversalPackageMetadata info)
        {
            if (info.Group != null)
            {
                if (info.Group.Length > 250)
                {
                    return("group must be between 0 and 250 characters long.");
                }

                var invalid = info.Group.Where(c => (c <'0' || c> '9') && (c <'a' || c> 'z') && (c <'A' || c> 'Z') && c != '-' && c != '.' && c != '/' && c != '_').Distinct().ToArray();
                if (invalid.Length == 1)
                {
                    return("group contains invalid character: '" + invalid[0] + "'");
                }
                else if (invalid.Length > 1)
                {
                    return("group contains invalid characters: '" + string.Join("', '", invalid) + "'");
                }

                if (info.Group.StartsWith("/") || info.Group.EndsWith("/"))
                {
                    return("group must not start or end with a slash.");
                }
            }

            {
                if (string.IsNullOrEmpty(info.Name))
                {
                    return("missing name.");
                }
                if (info.Name.Length > 50)
                {
                    return("name must be between 1 and 50 characters long.");
                }

                var invalid = info.Name.Where(c => (c <'0' || c> '9') && (c <'a' || c> 'z') && (c <'A' || c> 'Z') && c != '-' && c != '.' && c != '_').Distinct().ToArray();
                if (invalid.Length == 1)
                {
                    return("name contains invalid character: '" + invalid[0] + "'");
                }
                else if (invalid.Length > 1)
                {
                    return("name contains invalid characters: '" + string.Join("', '", invalid) + "'");
                }
            }

            if (info.Version == null)
            {
                return("missing or invalid version.");
            }

            if (info.Title != null && info.Title.Length > 50)
            {
                return("title must be between 0 and 50 characters long.");
            }

            return(null);
        }
Example #2
0
        internal static void PrintManifest(UniversalPackageMetadata info)
        {
            if (!string.IsNullOrEmpty(info.Group))
            {
                Console.WriteLine($"Package: {info.Group}/{info.Name}");
            }
            else
            {
                Console.WriteLine($"Package: {info.Name}");
            }

            Console.WriteLine($"Version: {info.Version}");
        }
Example #3
0
        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);
            }

            UniversalPackageMetadata info;

            if (string.IsNullOrWhiteSpace(this.Manifest))
            {
                info = new UniversalPackageMetadata
                {
                    Group       = this.Group,
                    Name        = this.Name,
                    Version     = UniversalPackageVersion.TryParse(this.Version),
                    Title       = this.Title,
                    Description = this.PackageDescription,
                    Icon        = this.IconUrl
                };
            }
            else
            {
                if (!File.Exists(this.Manifest))
                {
                    Console.Error.WriteLine($"The manifest file '{this.Manifest}' does not exist.");
                    return(2);
                }

                using (var metadataStream = File.OpenRead(this.Manifest))
                {
                    info = await ReadManifestAsync(metadataStream);
                }
            }

            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)
            {
                info["createdDate"] = DateTime.UtcNow.ToString("u");
                if (!string.IsNullOrEmpty(this.Note))
                {
                    info["createdReason"] = this.Note;
                }
                info["createdUsing"] = "upack/" + typeof(Pack).Assembly.GetName().Version.ToString(3);
                info["createdBy"]    = Environment.UserName;
            }

            if (!Directory.Exists(this.SourcePath) && !File.Exists(this.SourcePath))
            {
                Console.Error.WriteLine($"The source directory '{this.SourcePath}' does not exist.");
                return(2);
            }

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

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

            string tmpPath = Path.GetTempFileName();

            using (var builder = new UniversalPackageBuilder(tmpPath, info))
            {
                if (Directory.Exists(this.SourcePath))
                {
                    await builder.AddContentsAsync(
                        this.SourcePath,
                        "/",
                        true,
                        s => string.IsNullOrWhiteSpace(this.Manifest) || !string.Equals(s, "upack.json", StringComparison.OrdinalIgnoreCase),
                        cancellationToken
                        );
                }
                else
                {
                    using (var file = File.Open(this.SourcePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        await builder.AddFileAsync(file, Path.GetFileName(this.SourcePath), File.GetLastWriteTimeUtc(this.SourcePath), cancellationToken);
                    }
                }
            }

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

            return(0);
        }