Beispiel #1
0
        public static int Main(string[] args)
        {
            Serializer.Setup();

            // Determine what operation to perform.
            INapackOperation operation = NapackOperationFinder.FindOperation(args);

            if (operation == null)
            {
                NapackOperationFinder.WriteGeneralUsageToConsole();
                return(ERROR);
            }

            try
            {
                operation.PerformOperation();
            }
            catch (Exception ex)
            {
                NapackClient.Log(ex.Message);
                return(ERROR);
            }

            return(SUCCESS);
        }
Beispiel #2
0
        private async Task SaveNapackAsync(NapackVersionIdentifier versionId, NapackVersion version)
        {
            string enclosingDirectory = Path.Combine(this.NapackDirectory, versionId.GetFullName());

            Directory.CreateDirectory(enclosingDirectory);

            List <Task> fileWriteTasks = new List <Task>();

            foreach (KeyValuePair <string, NapackFile> file in version.Files)
            {
                fileWriteTasks.Add(this.SaveFileAsync(Path.Combine(enclosingDirectory, file.Key), file.Value.Contents));
            }

            fileWriteTasks.Add(GenerateTargetAsync(versionId, version.Files.ToDictionary(item => item.Key, item => item.Value.MsbuildType)));

            try
            {
                await Task.WhenAll(fileWriteTasks).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                NapackClient.Log("Error saving napack file: " + ex.Message);
                throw;
            }
        }
        public static void WriteGeneralUsageToConsole()
        {
            const string separator = "******";

            NapackClient.Log("Operations: ");
            foreach (Type operationType in NapackOperationFinder.KnownOperations)
            {
                NapackClient.Log(separator + operationType.Name + separator);
                CommandLineParser parser = new CommandLineParser(operationType);

                StringBuilder builder = new StringBuilder();
                using (TextWriter writer = new StringWriter(builder))
                {
                    parser.WriteUsage(writer, 0);
                }

                string[] lines = builder.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string line in lines)
                {
                    NapackClient.Log(line);
                }

                NapackClient.Log(separator + new string('*', operationType.Name.Length) + separator);
            }
        }
Beispiel #4
0
        private void AcquireNapacks(NapackServerClient client, List <NapackVersionIdentifier> listedNapacks, Dictionary <string, List <NapackVersion> > knownNapacks)
        {
            List <NapackVersionIdentifier> newNapacks = new List <NapackVersionIdentifier>();

            foreach (NapackVersionIdentifier listedNapack in listedNapacks)
            {
                if (!knownNapacks.Any(napack =>
                                      napack.Key.Equals(listedNapack.NapackName, StringComparison.InvariantCulture) &&
                                      napack.Value.Any(individualPack => individualPack.Major == listedNapack.Major)))
                {
                    // No napack was found with the specified name and major version.
                    newNapacks.Add(listedNapack);
                }
            }

            NapackClient.Log($"Found {newNapacks.Count} new napacks to download.");


            // Ah, for the want of a multiple return...
            List <Tuple <NapackVersionIdentifier, NapackVersion> > newDownloadedNapacks = this.DownloadNewNapacks(client, newNapacks);
            List <NapackMajorVersion> dependencies = TabulateNewDependencies(knownNapacks, newDownloadedNapacks);

            int level = 0;

            NapackClient.Log("Processed dependency level " + level + " with " + dependencies.Count + " new dependencies found.");

            while (dependencies.Any())
            {
                newDownloadedNapacks = DownloadDependentNapacks(client, dependencies);
                dependencies         = TabulateNewDependencies(knownNapacks, newDownloadedNapacks);

                ++level;
                NapackClient.Log("Processed dependency level " + level);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Remove folders that aren't napacks or napack major versions that aren't referenced.
        /// </summary>
        /// <returns>The known napacks, with napacks that don't exist removed.</returns>
        private Dictionary <string, List <NapackVersion> > RemoveUnusedNapacks(IDictionary <string, List <NapackVersion> > knownNapacks)
        {
            if (!Directory.Exists(this.NapackDirectory))
            {
                Directory.CreateDirectory(this.NapackDirectory);
            }

            int unknownFolders = 0;
            int unusedNapacks  = 0;
            List <NapackVersionIdentifier> versions = new List <NapackVersionIdentifier>();

            foreach (string directory in Directory.EnumerateDirectories(this.NapackDirectory))
            {
                string napackDirectoryName            = Path.GetFileName(directory);
                NapackVersionIdentifier napackVersion = null;
                try
                {
                    napackVersion = new NapackVersionIdentifier(napackDirectoryName);
                }
                catch (Exception ex)
                {
                    NapackClient.Log("Directory is not a Napack; removing " + napackDirectoryName);
                    NapackClient.Log(ex.Message);
                    Directory.Delete(directory, true);
                    ++unknownFolders;
                }

                if (!knownNapacks.ContainsKey(napackVersion.NapackName) ||
                    !knownNapacks[napackVersion.NapackName].Any(version => version.Major == napackVersion.Major))
                {
                    // This is an unknown napack or major version. Delete.
                    NapackClient.Log("Napack is not used, deleting: " + napackDirectoryName);
                    Directory.Delete(directory, true);
                    ++unusedNapacks;
                }

                versions.Add(napackVersion);
            }

            Dictionary <string, List <NapackVersion> > redactedKnownNapacks = new Dictionary <string, List <NapackVersion> >();

            foreach (KeyValuePair <string, List <NapackVersion> > napack in knownNapacks)
            {
                foreach (NapackVersion version in napack.Value)
                {
                    if (versions.Any(ver => ver.NapackName.Equals(napack.Key) && ver.Major == version.Major))
                    {
                        if (!redactedKnownNapacks.ContainsKey(napack.Key))
                        {
                            redactedKnownNapacks.Add(napack.Key, new List <NapackVersion>());
                        }

                        redactedKnownNapacks[napack.Key].Add(version);
                    }
                }
            }

            NapackClient.Log($"Deleted {unknownFolders} unknown folders and {unusedNapacks} unused napacks.");
            return(redactedKnownNapacks);
        }
Beispiel #6
0
        public void PerformOperation()
        {
            // The specified napacks.
            NapackClient.Log("Reading in the Napacks JSON file...");
            Dictionary <string, string>    rawNapacks    = Serializer.Deserialize <Dictionary <string, string> >(File.ReadAllText(this.NapacksFile));
            List <NapackVersionIdentifier> listedNapacks = rawNapacks.Select(item => new NapackVersionIdentifier(item.Key + "." + item.Value)).ToList();

            // The specified napacks, plus those dependencies. This doesn't have to exist -- we'll detect everything as unused if it doesn't and clear the napack folder.
            Dictionary <string, List <NapackVersion> > knownNapacks = new Dictionary <string, List <NapackVersion> >();

            try
            {
                knownNapacks = Serializer.Deserialize <Dictionary <string, List <NapackVersion> > >(File.ReadAllText(this.NapacksFile + UpdateOperation.LockFileSuffix));
            }
            catch (Exception)
            {
                NapackClient.Log("Didn't find the napack lock file; packages may be redownloaded.");
            }

            NapackClient.Log("Reading in the Napack Settings JSON file...");
            NapackClientSettings settings = Serializer.Deserialize <NapackClientSettings>(File.ReadAllText(this.NapackSettingsFile));

            // Now that we've read in everything, start processing.
            knownNapacks = this.RemoveUnusedNapacks(knownNapacks);

            using (NapackServerClient client = new NapackServerClient(settings.NapackFrameworkServer))
            {
                this.AcquireNapacks(client, listedNapacks, knownNapacks);
            }

            // Save the lock file now that we're in an updated state.
            foreach (NapackVersion knownNapack in knownNapacks.Values.SelectMany(value => value))
            {
                // Clear the files so we aren't duplicating them in the lock file.
                knownNapack.Files.Clear();
            }
            File.WriteAllText(this.NapacksFile + UpdateOperation.LockFileSuffix, Serializer.Serialize(knownNapacks));

            // Extract out our version identifiers
            List <NapackVersionIdentifier>     napackVersionIdentifiers = new List <NapackVersionIdentifier>();
            Dictionary <string, NapackVersion> allNapackVersions        = new Dictionary <string, NapackVersion>();

            foreach (KeyValuePair <string, List <NapackVersion> > napackVersions in knownNapacks)
            {
                foreach (NapackVersion napackVersion in napackVersions.Value)
                {
                    napackVersionIdentifiers.Add(new NapackVersionIdentifier(napackVersions.Key, napackVersion.Major, napackVersion.Minor, napackVersion.Patch));
                    allNapackVersions.Add(napackVersions.Key, napackVersion);
                }
            }

            NapackTargets.SaveNapackTargetsFile(this.ProjectDirectory, this.NapackDirectory, napackVersionIdentifiers);
            NapackAttributions.SaveNapackAttributionsFile(this.ProjectDirectory, allNapackVersions);
        }
Beispiel #7
0
        public void PerformOperation()
        {
            NapackClientSettings settings = Serializer.Deserialize <NapackClientSettings>(File.ReadAllText(this.NapackSettingsFile));

            using (NapackServerClient client = new NapackServerClient(settings.NapackFrameworkServer))
            {
                string response = client.VerifyUserAsync(this.UserEmail, this.VerificationCode).GetAwaiter().GetResult();
                NapackClient.Log("Verification status:");
                NapackClient.Log(response);
            }
        }
Beispiel #8
0
        private async Task CreateNapackPackageAsync(string packageName, NapackLocalDescriptor napackDescriptor, Dictionary <string, NapackFile> files, UserSecret secret, NapackServerClient client)
        {
            NewNapack newNapack = new NewNapack()
            {
                metadata = new NewNapackMetadata()
                {
                    Description       = napackDescriptor.Description,
                    MoreInformation   = napackDescriptor.MoreInformation,
                    AuthorizedUserIds = napackDescriptor.AuthorizedUserIds,
                    Tags = napackDescriptor.Tags
                },
                NewNapackVersion = this.CreateNapackVersion(napackDescriptor, files)
            };

            string response = await client.CreatePackageAsync(packageName, newNapack, secret).ConfigureAwait(false);

            NapackClient.Log($"Package creation result: {response}");
        }
Beispiel #9
0
        public void PerformOperation()
        {
            NapackClientSettings settings = Serializer.Deserialize <NapackClientSettings>(File.ReadAllText(this.NapackSettingsFile));

            using (NapackServerClient client = new NapackServerClient(settings.NapackFrameworkServer))
            {
                UserSecret secret = client.RegisterUserAsync(this.UserEmail).GetAwaiter().GetResult();
                NapackClient.Log($"User {secret.UserId} successfully registered. Secrets (order&case sensitive):");
                foreach (Guid individualSecret in secret.Secrets)
                {
                    NapackClient.Log($"  {individualSecret}");
                }

                if (this.SaveAsDefault)
                {
                    File.WriteAllText(NapackClient.GetDefaultCredentialFilePath(), Serializer.Serialize(new DefaultCredentials(secret.UserId, secret.Secrets)));
                }
            }
        }
Beispiel #10
0
        private async Task CreateOrUpdateNapackAsync(string packageName, NapackLocalDescriptor napackDescriptor, UserSecret userSecret, NapackServerClient client)
        {
            string rootDirectory = Path.GetFullPath(Path.GetDirectoryName(this.PackageFile));
            Dictionary <string, NapackFile> files = UploadOperation.ParseNapackFiles(rootDirectory, rootDirectory);

            files.Remove(Path.GetFileName(this.PackageFile));

            bool packageExists = await client.ContainsNapack(packageName).ConfigureAwait(false);

            if (!packageExists)
            {
                await this.CreateNapackPackageAsync(packageName, napackDescriptor, files, userSecret, client).ConfigureAwait(false);
            }
            else
            {
                // This is a new version creation operation.
                VersionDescriptor version = await client.UpdatePackageAsync(packageName, this.CreateNapackVersion(napackDescriptor, files), userSecret).ConfigureAwait(false);

                NapackClient.Log($"Updated the {packageName} package to version {version.Major}.{version.Minor}.{version.Patch}");
            }
        }
Beispiel #11
0
        public void PerformOperation()
        {
            NapackClient.Log("Reading in the Napack settings file...");
            NapackClientSettings settings = Serializer.Deserialize <NapackClientSettings>(File.ReadAllText(this.NapackSettingsFile));

            string      userId     = this.UserId;
            List <Guid> accessKeys = this.AuthorizationKeys?.Split(';').Select(key => Guid.Parse(key)).ToList();

            if (string.IsNullOrWhiteSpace(this.UserId) || accessKeys == null || accessKeys.Count == 0)
            {
                DefaultCredentials defaultCredentials = Serializer.Deserialize <DefaultCredentials>(File.ReadAllText(NapackClient.GetDefaultCredentialFilePath()));
                userId     = defaultCredentials.UserId;
                accessKeys = defaultCredentials.Secrets;
            }

            UserSecret userSecret = new UserSecret()
            {
                UserId  = userId,
                Secrets = accessKeys
            };

            string packageName = Path.GetFileNameWithoutExtension(this.PackageFile);
            NapackLocalDescriptor napackDescriptor = Serializer.Deserialize <NapackLocalDescriptor>(File.ReadAllText(this.PackageFile));

            napackDescriptor.Validate(userId, false);

            using (NapackServerClient client = new NapackServerClient(settings.NapackFrameworkServer))
            {
                NewNapackMetadata metadata = new NewNapackMetadata()
                {
                    AuthorizedUserIds = napackDescriptor.AuthorizedUserIds,
                    Description       = napackDescriptor.Description,
                    MoreInformation   = napackDescriptor.MoreInformation,
                    Tags = napackDescriptor.Tags
                };

                string response = client.UpdatePackageMetadataAsync(packageName, metadata, userSecret).GetAwaiter().GetResult();
                NapackClient.Log($"Package metadata update result: {response}");
            }
        }
Beispiel #12
0
        private List <Tuple <NapackVersionIdentifier, NapackVersion> > DownloadNapacks <T>(List <T> newNapacks,
                                                                                           Func <T, Task <NapackVersion> > downloadOperation, Func <T, NapackVersion, NapackVersionIdentifier> identifierOperation)
        {
            Stopwatch timer = Stopwatch.StartNew();
            List <Task <Tuple <NapackVersionIdentifier, NapackVersion> > > napackDownloadTasks =
                new List <Task <Tuple <NapackVersionIdentifier, NapackVersion> > >();

            foreach (T napackItem in newNapacks)
            {
                napackDownloadTasks.Add(Task.Run(async() =>
                {
                    NapackVersion version = await downloadOperation(napackItem).ConfigureAwait(false);
                    NapackVersionIdentifier identifier = identifierOperation(napackItem, version);
                    await this.SaveNapackAsync(identifier, version).ConfigureAwait(false);
                    return(Tuple.Create <NapackVersionIdentifier, NapackVersion>(identifier, version));
                }));
            }

            Task.WhenAll(napackDownloadTasks).GetAwaiter().GetResult();

            NapackClient.Log("Downloaded " + newNapacks.Count + " in " + timer.ElapsedMilliseconds + " ms.");
            return(napackDownloadTasks.Select(task => task.Result).ToList());
        }
Beispiel #13
0
        public void PerformOperation()
        {
            NapackClientSettings settings = Serializer.Deserialize <NapackClientSettings>(File.ReadAllText(this.NapackSettingsFile));

            string      userId     = this.UserId;
            List <Guid> accessKeys = this.AuthorizationKeys?.Split(';').Select(key => Guid.Parse(key)).ToList();

            if (string.IsNullOrWhiteSpace(this.UserId) || accessKeys == null || accessKeys.Count == 0)
            {
                DefaultCredentials defaultCredentials = Serializer.Deserialize <DefaultCredentials>(File.ReadAllText(NapackClient.GetDefaultCredentialFilePath()));
                userId     = defaultCredentials.UserId;
                accessKeys = defaultCredentials.Secrets;
            }

            UserSecret userSecret = new UserSecret()
            {
                UserId  = userId,
                Secrets = accessKeys
            };

            string packageName = Path.GetFileNameWithoutExtension(this.PackageFile);
            NapackLocalDescriptor napackDescriptor = Serializer.Deserialize <NapackLocalDescriptor>(File.ReadAllText(this.PackageFile));

            napackDescriptor.Validate(userId, true);

            using (NapackServerClient client = new NapackServerClient(settings.NapackFrameworkServer))
            {
                CreateOrUpdateNapackAsync(packageName, napackDescriptor, userSecret, client).GetAwaiter().GetResult();
            }
        }