예제 #1
0
파일: Project.cs 프로젝트: HJfod/GDMake
        public Result <string> Save(bool mkdir = false, bool mkexample = true)
        {
            if (!Directory.Exists(this.Dir))
            {
                if (mkdir)
                {
                    try { Directory.CreateDirectory(this.Dir); }
                    catch (Exception e) {
                        return(new ErrorResult <string>($"Unable to create directory {this.Dir}: {e.Message}"));
                    }
                }
                else
                {
                    return(new ErrorResult <string>($"Directory {this.Dir} does not exist! (Use --mkdir to create it)"));
                }
            }

            this.Dotfile = new GDMakeFile(this.Name);
            var dotfile_str = JsonSerializer.Serialize(
                this.Dotfile, new JsonSerializerOptions {
                WriteIndented = true
            }
                );

            bool alreadyExists = File.Exists(Path.Join(this.Dir, GDMake.DotfileName));

            try { File.WriteAllText(Path.Join(this.Dir, GDMake.DotfileName), dotfile_str); }
            catch (Exception e) {
                return(new ErrorResult <string>($"Unable to create {GDMake.DotfileName} file: {e.Message}"));
            }

            if (mkexample && !File.Exists(Path.Join(this.Dir, "main.cpp")))
            {
                File.WriteAllText(Path.Join(this.Dir, "main.cpp"), DefaultStrings.ExampleProjectCpp);
            }

            if (alreadyExists)
            {
                return(new SuccessResult <string>($"Reinitialized as {this.Name} in {this.Dir}!"));
            }

            return(new SuccessResult <string>($"Initialized as {this.Name} in {this.Dir}!"));
        }
예제 #2
0
파일: Project.cs 프로젝트: HJfod/GDMake
        public Result Load()
        {
            if (!Directory.Exists(this.Dir))
            {
                return(new ErrorResult($"Directory {this.Dir} does not exist!"));
            }

            if (!File.Exists(Path.Join(this.Dir, GDMake.DotfileName)))
            {
                return(new ErrorResult($"Directory {this.Dir} does not contain a {GDMake.DotfileName} file! " +
                                       "Use \"gdmake init\" to initialize a project."));
            }

            this.Dotfile = JsonSerializer.Deserialize <GDMakeFile>(
                File.ReadAllText(Path.Join(this.Dir, GDMake.DotfileName))
                );

            this.Name = this.Dotfile.ProjectName;

            return(new SuccessResult());
        }