public static void GetAllOptions() { var verbTypes = (from type in Assembly.GetExecutingAssembly().GetTypes() let testAttribute = Attribute.GetCustomAttribute(type, typeof(VerbAttribute)) where testAttribute != null select type).ToList(); foreach (var item in verbTypes) { AllOptions.Add(item.CustomAttributes.ToList() .FirstOrDefault().ConstructorArguments.ToList().FirstOrDefault().Value.ToString(), item.CustomAttributes.ToList() .FirstOrDefault().NamedArguments.ToList().FirstOrDefault().TypedValue.ToString()); } PrintTable table = new PrintTable { { "Action Name", 0 }, { "Action Description", 0 }, }; foreach (KeyValuePair <string, string> descriptionKVPair in AllOptions) { string actionName = descriptionKVPair.Key; string description = descriptionKVPair.Value; table.addRow( actionName, description ); } Logger.logTable(table, visibleLines: false); }
//private static Constants.ResourceType? listType; static int Main(string[] args) { Parser parser = new Parser(settings => { settings.CaseSensitive = false; // settings.IgnoreUnknownArguments = false; }); PrintTable table = new PrintTable { { Constants.HeaderStringType, 6 }, { Constants.HeaderStringNameOfResource, 21 }, { Constants.HeaderStringVersion, 16 }, { Constants.HeaderStringFileSize, 11 }, }; if (args.ToList().Count == 0) { Logger.logError($"{Constants.APPLICATION_ALIAS} must be called with an action name. Available actions:"); GetOptions.GetAllOptions(); return(0); } return(CommandLine.Parser.Default.ParseArguments <InitOptions, AddOptions, ListOptions, AvailableOptions, DependencyOption, PublishOptions, RemoveOptions>(args.ToList()) .MapResult( (InitOptions opts) => InitOptions.initDirectories(), (AddOptions opts) => AddOptions.AddPackage(args.ToList()), (ListOptions opts) => ListOptions.ListResources(opts.resourceType, args.ToList()), (AvailableOptions opts) => AvailableOptions.Available(), (DependencyOption opts) => DependencyOption.ListAllDependencies(args.ToList()), (PublishOptions opts) => PublishOptions.Publish(args.ToList()), (RemoveOptions opts) => RemoveOptions.RemovePackage(args.ToList()), errs => 1)); }
public static void lstResources(Constants.ResourceType?listType) { try { if (!ResourceTypes.hasNecessaryDirs()) { Logger.logError($"Missing some or all resource directories in current directory. Try running {Constants.APPLICATION_ALIAS} init?"); return; } PrintTable table = new PrintTable { { Constants.HeaderStringType, 6 }, { Constants.HeaderStringNameOfResource, 21 }, { Constants.HeaderStringVersion, 16 }, { Constants.HeaderStringFileSize, 11 }, }; List <Constants.ResourceType> resourcesToList = listType.HasValue ? new List <Constants.ResourceType> { listType.Value } : new List <Constants.ResourceType> { Constants.ResourceType.Code, Constants.ResourceType.Data, Constants.ResourceType.Model }; foreach (Constants.ResourceType resourceType in resourcesToList) { foreach (string resourceName in ResourceTypes.resourceNames(resourceType)) { string version; if (ResourceTypes.resourceVersionFileExists(resourceType, resourceName)) { version = ResourceTypes.getResourceVersion(resourceType, resourceName); } else { version = "Unknown version"; } long fileSize = ResourceTypes.getResourceSize(resourceType, resourceName); table.addRow( PrintTable.doFormat(resourceType.ToString()), PrintTable.doFormat(resourceName), PrintTable.doFormat(version), PrintTable.doFormat(ResourceTypes.bytesToString(fileSize)) ); } } Logger.logTable(table); } catch { } }
public static void Add_Package(string prefix, ResourceIdentifier resourceDescription) { try { ResourceType resourceType = resourceDescription.resourceType; string resourceName = resourceDescription.resourceName; // check if the resource is available from the server var availableResources = NetworkUtils.getAvailableResources(prefix, resourceType); if (!availableResources.resourceDescriptions.ContainsKey(resourceName)) { Logger.logError($"No resource called {resourceName} is available from the server."); return; } string version = resourceDescription.version; // possible null var serverVersionInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName); if (version == null) { version = serverVersionInfo.latestVersion; } else { // check that the requested version is available from the server if (!serverVersionInfo.versions.ContainsKey(version)) { Logger.logError( $"There is no version {version} available of resource {resourceName}. " + $"These are the version(s) available: {string.Join(", ", serverVersionInfo.versions.Keys.ToList())}" ); return; } } // check if nyoka directories exists if (!ResourceTypes.hasNecessaryDirs()) { bool createDirs = Logger.askYesOrNo( "Resource directories are not present in this directory. Create them now?" ); if (createDirs) { InitOptions.tryCreateDirIfNonExistent(); } else { Logger.logLine("Package add aborted"); return; } } // check if the resource is already present if (ResourceTypes.resourceFileExists(resourceType, resourceName)) { bool continueAnyways = Logger.askYesOrNo( $"{resourceType.ToString()} resource {resourceName} is already present. Delete and replace ?" ); if (continueAnyways) { ResourceTypes.removeResourceFilesIfPresent(resourceType, resourceName); } else { Logger.logLine("Aborting resource add."); return; } } ResourceDependencyInfoContainer dependencies = NetworkUtils.getResourceDependencies(prefix, resourceType, resourceName, version); var depDescriptions = new Dictionary <ResourceType, Dictionary <string, ResourceDependencyInfoContainer.DependencyDescription> > { { ResourceType.Code, dependencies.codeDeps }, { ResourceType.Data, dependencies.dataDeps }, { ResourceType.Model, dependencies.modelDeps }, }; bool downloadDependencies = false; // if there package has any dependencies if (depDescriptions.Any(kvPair => kvPair.Value.Count != 0)) { PrintTable table = new PrintTable { { "Resource Type", 13 }, { "Dependency Type", 15 }, { "Name of Resource", 16 }, { "Resource Version", 16 }, { "File Size", 9 }, }; foreach (var(depResourceType, deps) in depDescriptions.Select(x => (x.Key, x.Value))) { foreach (var(depName, depDescription) in deps.Select(x => (x.Key, x.Value))) { table.addRow( depResourceType.ToString(), depDescription.isDirectDependency ? "direct" : "indirect", depName, depDescription.versionStr, ResourceTypes.bytesToString(depDescription.byteCount) ); } } Logger.logLine($"Resource {resourceName} has these dependencies:"); Logger.logTable(table); downloadDependencies = Logger.askYesOrNo("Download these dependencies?"); if (downloadDependencies) { Logger.logLine("Downloading dependencies"); } else { Logger.logLine("Skipping downloading dependencies."); } } if (downloadDependencies) { var depsToDownload = new List <(ResourceType, string, string)>(); foreach (var(depResourceType, deps) in depDescriptions.Select(x => (x.Key, x.Value))) { foreach (var(depName, depDescription) in deps.Select(x => (x.Key, x.Value))) { bool continueWithDownload = true; // Ask user whether to overwrite file if a file with this name exists locally already if (ResourceTypes.resourceFileExists(depResourceType, depName)) { if (ResourceTypes.resourceVersionFileExists(depResourceType, depName)) { string depLocalVersion = ResourceTypes.getResourceVersion(depResourceType, depName); if (depDescription.versionStr == depLocalVersion) { continueWithDownload = Logger.askYesOrNo( $"Dependency {depName} file exists locally at the required version " + $"({depDescription.versionStr}). Overwrite this file?" ); } else { continueWithDownload = Logger.askYesOrNo( $"Dependency {depName} file exists locally at version {depLocalVersion}" + $" (depency required version is {depDescription.versionStr}). Overwrite this file?" ); } } else { continueWithDownload = Logger.askYesOrNo( $"Dependency {depName} file exists locally at an unknown version. Overwrite this file?" ); } } if (continueWithDownload) { depsToDownload.Add((depResourceType, depName, depDescription.versionStr)); } else { Logger.logWarning($"Skipping download of dependency {depName}."); } } } foreach (var(depResourceType, depName, depVersion) in depsToDownload) { downloadPackage(prefix, depResourceType, depName, depVersion); } } downloadPackage(prefix, resourceType, resourceName, version); } catch (NetworkUtils.NetworkUtilsException ex) { Logger.logError($"Network Error: {ex.Message}"); } catch (Exception ex) { Logger.logError($"File System Error: " + ex.Message); } }
public static void listDependencies(string prefix, ResourceIdentifier resourceDescription) { string resourceName = resourceDescription.resourceName; ResourceType resourceType = resourceDescription.resourceType; string version = resourceDescription.version; try { // check if this resource exists on server var availableResources = NetworkUtils.getAvailableResources(prefix, resourceType); if (!availableResources.resourceDescriptions.ContainsKey(resourceName)) { Logger.logError($"{resourceType.ToString()} resource {resourceName} could not be found on server"); return; } if (version == null) { if ( ResourceTypes.resourceFileExists(resourceType, resourceName) && ResourceTypes.resourceVersionFileExists(resourceType, resourceName) ) { version = ResourceTypes.getResourceVersion(resourceType, resourceName); } else { var versionInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName); version = versionInfo.latestVersion; } } // check if user-specified version exists on the server at the given version else { var versionInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName); if (!versionInfo.versions.ContainsKey(version)) { Logger.logError("Server does not report having a version \"{version}\" available for {resourceName}"); } } Logger.logLine($"Showing dependencies of {resourceName}, version {version}"); ResourceDependencyInfoContainer deps = NetworkUtils.getResourceDependencies(prefix, resourceType, resourceName, version); PrintTable table = new PrintTable { { "Resource Type", 13 }, { "Dependency Type", 15 }, { "Name of Resource", 15 }, { "Resource Version", 15 }, { "File Size", 10 }, }; var availableResourcesInfo = new Dictionary <ResourceType, AvailableResourcesInfoContainer> { { ResourceType.Code, NetworkUtils.getAvailableResources(prefix, ResourceType.Code) }, { ResourceType.Data, NetworkUtils.getAvailableResources(prefix, ResourceType.Data) }, { ResourceType.Model, NetworkUtils.getAvailableResources(prefix, ResourceType.Model) }, }; var showDepDict = new Dictionary <ResourceType, Dictionary <string, ResourceDependencyInfoContainer.DependencyDescription> >() { { ResourceType.Code, deps.codeDeps }, { ResourceType.Data, deps.dataDeps }, { ResourceType.Model, deps.modelDeps }, }; foreach (var(dependenciesType, descriptions) in showDepDict.Select(x => (x.Key, x.Value))) { foreach (var(dependencyName, dependencyDescription) in descriptions.Select(x => (x.Key, x.Value))) { table.addRow( dependenciesType.ToString(), dependencyDescription.isDirectDependency ? "direct" : "indirect", dependencyName, dependencyDescription.versionStr, ResourceTypes.bytesToString(availableResourcesInfo[dependenciesType].resourceDescriptions[dependencyName].byteCount) ); } } Logger.logTable(table); } catch (NetworkUtils.NetworkUtilsException ex) { Logger.logError($"Network Error: {ex.Message}"); } catch (Exception ex) { Logger.logError($"File System Error: " + ex.Message); } }
public static void listAvailableResources(string prefix, ResourceType?listType) { try { List <ResourceType> resourcesToList = listType.HasValue ? new List <ResourceType> { listType.Value } : new List <ResourceType> { ResourceType.Code, ResourceType.Data, ResourceType.Model }; PrintTable printTable = new PrintTable { { Constants.HeaderStringType, 6 }, { Constants.HeaderStringNameOfResource, 21 }, { Constants.HeaderStringLatestVersion, 16 }, { Constants.HeaderStringLocalVersion, 2 }, { Constants.HeaderStringFileSize, 11 }, }; foreach (ResourceType resourceType in resourcesToList) { var availableResources = NetworkUtils.getAvailableResources(prefix, resourceType); foreach (string resourceName in availableResources.resourceDescriptions.Keys.OrderBy(k => k)) { string localVersionStr; bool resourceExistsLocally = ResourceTypes.resourceFileExists(resourceType, resourceName); if (resourceExistsLocally) { if (ResourceTypes.resourceVersionFileExists(resourceType, resourceName)) { localVersionStr = ResourceTypes.getResourceVersion(resourceType, resourceName); } else { localVersionStr = "Unknown version"; } } else { localVersionStr = "Not present"; } printTable.addRow( PrintTable.doFormat(resourceType.ToString()), PrintTable.doFormat(resourceName), PrintTable.doFormat(availableResources.resourceDescriptions[resourceName].versionStr), PrintTable.doFormat(localVersionStr), PrintTable.doFormat(ResourceTypes.bytesToString(availableResources.resourceDescriptions[resourceName].byteCount)) ); } } Logger.logTable(printTable); } catch (NetworkUtils.NetworkUtilsException ex) { Logger.logError($"Network Error: {ex.Message}"); } catch (Exception ex) { Logger.logError($"File System Error: " + ex.Message); } }