Esempio n. 1
0
        void Create(IList <string> args)
        {
            Task.Run(
                () => _fuse.ConnectOrSpawn("Fuse create", Timeout.InfiniteTimeSpan));

            var argIdx       = 0;
            var templateName = args.TryGetAt(argIdx++)
                               .OrThrow(new ExitWithError(Usage));

            var name = args.TryGetAt(argIdx++).Or("Untitled");

            var validatedName = FileName.Validate(name);

            if (!validatedName.HasValue)
            {
                throw new ExitWithError(validatedName.Error.Capitalize());
            }

            try
            {
                var destPath = args.TryGetAt(argIdx++).Select(
                    p => (_fileSystem.ResolveAbsolutePath(p) as AbsoluteDirectoryPath).ToOptional()
                    .OrThrow(new ExitWithError("Invalid project path" + ": " + args[argIdx - 1])));

                var spawnTemplate   = new SpawnTemplate(_fileSystem);
                var projectTemplate = _projectTemplates().FirstOrDefault(t => TemplateNameEquals(t, templateName));
                var fileTemplate    = _fileTemplates().FirstOrDefault(t => TemplateNameEquals(t, templateName));

                if (projectTemplate == null && fileTemplate == null)
                {
                    throw new ExitWithError("Unknown template name, see fuse help create for a list of valid template names.");
                }

                var templateIsProjectTemplate = projectTemplate != null;

                if (templateIsProjectTemplate)
                {
                    var resultPath = spawnTemplate.CreateProject(name, projectTemplate, destPath);
                    using (_textWriter.PushColor(ConsoleColor.Green))
                        _textWriter.WriteLine("Created project: '" + name + "' at '" + resultPath.NativePath + "'");
                }
                else
                {
                    var resultPath = spawnTemplate.CreateFile(name, fileTemplate, destPath)
                                     .OrThrow(new FailedToCreateFileFromTemplate("Failed to create file from template (unknown reason)"));
                    using (_textWriter.PushColor(ConsoleColor.Green))
                        _textWriter.WriteLine("Created file at '" + resultPath.NativePath + "'");
                }
            }
            catch (ProjectNotFound)
            {
                throw new ExitWithError("Could not find a project to put the file in, please check if destination folder or its parents contains a project.");
            }
            catch (FileAlreadyExist e)
            {
                throw new ExitWithError(e.Message);
            }
            catch (InvalidPath p)
            {
                throw new ExitWithError("Invalid project path" + ": " + p.Path);
            }
            catch (SecurityException e)
            {
                throw new ExitWithError(e.Message);
            }
            catch (DaemonException e)
            {
                throw new ExitWithError(e.Message);
            }
            catch (FailedToCreateFileFromTemplate e)
            {
                throw new ExitWithError(e.Message);
            }
            catch (ProjectFolderNotEmpty)
            {
                throw new ExitWithError("A folder with that name already exists, and it is not empty.");
            }
            catch (UnauthorizedAccessException e)
            {
                throw new ExitWithError(e.Message);
            }
            catch (IOException e)
            {
                throw new ExitWithError(e.Message);
            }
            catch (FailedToAddProjectToRecentList e)
            {
                throw new ExitWithError(e.Message);
            }
        }