Exemple #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);
            }
        }
Exemple #2
0
        IControl CreateContent(Template template, IDialog <bool> dialog)
        {
            var projectLocation =
                UserSettings.Folder("MostRecentlyUsedFolder")
                .Convert(v => v.Or(Optional.Some(_fuse.ProjectsDir)), d => d)
                .AutoInvalidate(TimeSpan.FromMilliseconds(100));
            var validatedLocation = projectLocation.Validate(v => v.NativePath, ValidateExistingDirectory);

            var projectName = Property.Create(
                Optional.Some(_shell.MakeUnique(projectLocation.Latest().First().Value / "App").Name));
            var validatedName =
                projectName.Validate(v => v.ToString(), DirectoryName.Validate);

            var possibleProjectName = projectLocation.CombineLatest(
                projectName,
                (loc, pro) => { return(loc.SelectMany(l => pro.Select(n => l / n))); })
                                      .Select(
                d => d.HasValue && Directory.Exists(d.Value.NativePath) ?
                Optional.Some("Project '" + d.Value.Name + "' already exists in " + d.Value.ContainingDirectory.NativePath) :
                Optional.None());

            return
                (Layout.Dock()
                 .Bottom(Layout.Dock()
                         .Right(
                             Buttons.DefaultButtonPrimary(
                                 text: "Create",
                                 cmd: Observable.CombineLatest(
                                     projectName, projectLocation,
                                     (name, location) =>
            {
                var projectDirectory =
                    location.SelectMany(l =>
                                        name.Select(n => l / n))
                    .Where(d => !_shell.Exists(d));

                return Command.Create(
                    isEnabled: projectDirectory.HasValue,
                    action: async() =>
                {
                    var spawnTemplate = new SpawnTemplate(_shell);
                    var resultPath = spawnTemplate.CreateProject(name.Value.ToString(), template, location.Value);
                    var projectPath = resultPath / new FileName(name.Value + ".unoproj");

                    await Application.OpenDocument(projectPath, showWindow: true);
                    dialog.Close(true);
                });
            }).Switch())
                             .WithWidth(104))
                         .Right(Buttons.DefaultButton(text: "Cancel", cmd: Command.Enabled(() => dialog.Close(false)))
                                .WithWidth(104)
                                .WithPadding(right: new Points(16)))
                         .Fill())

                 .Fill(
                     Layout.StackFromTop(
                         Label.Create("Name:", color: Theme.DefaultText)
                         .WithPadding(LabelThickness),
                         ValidatedTextBox.Create(validatedName)
                         .WithPadding(ControlPadding),
                         Control.Empty
                         .WithHeight(8),
                         Label.Create("Location:", color: Theme.DefaultText)
                         .WithPadding(LabelThickness),
                         Layout.Dock()
                         .Right(
                             Buttons.DefaultButton(text: "Browse", cmd: Command.Enabled(async() =>
            {
                var directory = await dialog.BrowseForDirectory(await projectLocation.FirstAsync().Or(DirectoryPath.GetCurrentDirectory()));
                if (directory.HasValue)
                {
                    projectLocation.Write(directory.Value, save: true);
                }
            }))
                             .WithWidth(80)
                             .WithHeight(22)
                             .CenterVertically()
                             .WithPadding(LabelThickness)
                             )
                         .Fill(ValidatedTextBox.Create(validatedLocation)
                               .WithPadding(ControlPadding)),
                         ErrorLabel.Create(possibleProjectName)
                         .WithPadding(ControlPadding)))
                 .WithPadding(ControlPadding)
                 .WithPadding(ControlPadding)
                 .WithBackground(Theme.PanelBackground));
        }