Пример #1
0
        public static async Task <int> ExecuteAsync(
            NewFileOptions fileOptions,
            IConsole console,
            IAppEnvironment appEnvironment,
            InvocationContext context = null)
        {
            var settingsManager = new EnvironmentSettingsManager(appEnvironment);
            EnvironmentSettings environmentSettings = settingsManager.LoadSettings();

            if (environmentSettings.WorkspacePath != null && fileOptions.FilePath != null)
            {
                console.Error.WriteLine("You must either set a workspace via the environment command or supply a filepath.");
                return(ReturnCodes.Error);
            }

            if (fileOptions.FilePath != null)
            {
                environmentSettings.WorkspacePath = fileOptions.FilePath.FullName;
            }

            List <ContentTypeConventionsRoot> conventions = await FindAllConventions(appEnvironment);

            // Select the convention that matches the template name specified
            ContentTypeConventionsRoot contentTypeConventionsRoot = conventions.FirstOrDefault(x => x.Conventions.Any(y => y.Conventions.Any(z => z.Value == fileOptions.TemplateName)));

            // Now find the filepath..
            Convention convention = contentTypeConventionsRoot?.Conventions.SelectMany(x => x.Conventions).FirstOrDefault(x => x.ContentType.StartsWith(ConventionContentTypes.FilePaths, StringComparison.CurrentCultureIgnoreCase));

            if (convention != null)
            {
                IVariableDirectoryPath variablePath = convention.Value.ToVariableDirectoryPath();

                if (variablePath.TryResolve(environmentSettings.ToKvPs(), out IAbsoluteDirectoryPath evaluatedPath) == VariablePathResolvingStatus.Success)
                {
                    IAbsoluteFilePath filepath = evaluatedPath.GetChildFileWithName($"post-{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.md");

                    if (!filepath.ParentDirectoryPath.Exists)
                    {
                        Directory.CreateDirectory(filepath.ParentDirectoryPath.ToString());
                    }

                    string template = Path.Join(contentTypeConventionsRoot.FilePath.ParentDirectoryPath.ParentDirectoryPath.ToString(), contentTypeConventionsRoot?.Conventions.FirstOrDefault()?.TemplatePath);

                    File.Copy(template, filepath.ToString());

                    console.Out.WriteLine($"Created: {filepath}");
                }
            }

            return(ReturnCodes.Ok);
        }
        public static Task <int> ExecuteAsync(
            SetOptions options,
            IConsole console,
            IAppEnvironment appEnvironment,
            InvocationContext context = null)
        {
            var settingsManager = new EnvironmentSettingsManager(appEnvironment);

            EnvironmentSettings settings = settingsManager.LoadSettings() ?? new EnvironmentSettings();

            if (options.Username != null)
            {
                settings.Username = options.Username.ToLowerInvariant();
            }

            if (options.WorkspacePath != null)
            {
                settings.WorkspacePath = options.WorkspacePath.FullName;
            }

            if (options.PublishPath != null)
            {
                settings.PublishPath = options.PublishPath.FullName;
            }

            if (options.Key != null && options.Value != null)
            {
                if (settings.Configuration.ContainsKey(options.Key))
                {
                    settings.Configuration[options.Key] = options.Value;
                }
                else
                {
                    settings.Configuration.Add(options.Key, options.Value);
                }
            }

            settingsManager.SaveSettings(settings);

            return(Task.FromResult(ReturnCodes.Ok));
        }