Procházet zdrojové kódy

More work on CLI, project serialization

Josh Engebretson před 10 roky
rodič
revize
2647ddbfa7

+ 1 - 0
Source/AtomicTool/AtomicTool.cpp

@@ -134,6 +134,7 @@ void AtomicTool::Start()
     // END LICENSE MANAGEMENT
 
     cmd->Run();
+
 }
 
 void AtomicTool::Stop()

+ 9 - 0
Source/ToolCore/Command/Command.cpp

@@ -1,3 +1,6 @@
+#include "../ToolSystem.h"
+#include "../Project/Project.h"
+
 #include "Command.h"
 
 namespace ToolCore
@@ -23,6 +26,12 @@ void Command::Error(const String& errorMsg)
 
 void Command::Finished()
 {
+    ToolSystem* tsystem = GetSubsystem<ToolSystem>();
+    Project* project = tsystem->GetProject();
+
+    if (project && project->IsDirty())
+        project->Save(project->GetProjectFilePath());
+
     SendEvent(E_COMMANDFINISHED);
 }
 

+ 19 - 4
Source/ToolCore/Command/PlatformAddCmd.cpp

@@ -4,6 +4,7 @@
 #include <Atomic/IO/File.h>
 
 #include "../ToolSystem.h"
+#include "../Project/Project.h"
 
 #include "PlatformAddCmd.h"
 
@@ -35,7 +36,7 @@ bool PlatformAddCmd::Parse(const Vector<String>& arguments, unsigned startIndex,
 
     if (!value.Length())
     {
-        errorMsg = "Unable to parse build platform";
+        errorMsg = "Unable to parse platform";
         return false;
     }
 
@@ -46,12 +47,26 @@ bool PlatformAddCmd::Parse(const Vector<String>& arguments, unsigned startIndex,
 
 void PlatformAddCmd::Run()
 {
-    LOGINFOF("Adding platform: %s", platformToAdd_.CString());
-
     ToolSystem* tsystem = GetSubsystem<ToolSystem>();
     Project* project = tsystem->GetProject();
 
-    Platform* platform = NULL;
+    Platform* platform = tsystem->GetPlatformByName(platformToAdd_);
+
+    if (!platform)
+    {
+        Error(ToString("Unknown platform: %s", platformToAdd_.CString()));
+        return;
+    }
+
+    if (project->ContainsPlatform(platform->GetPlatformID()))
+    {
+        Error(ToString("Project already contains platform: %s", platformToAdd_.CString()));
+        return;
+    }
+
+    LOGINFOF("Adding platform: %s", platformToAdd_.CString());
+
+    project->AddPlatform(platform->GetPlatformID());
 
     Finished();
 }

+ 44 - 110
Source/ToolCore/Project/Project.cpp

@@ -12,6 +12,11 @@
 
 #include <Atomic/Resource/JSONFile.h>
 
+#include "../Platform/Platform.h"
+
+#include "ProjectFile.h"
+#include "ProjectBuildSettings.h"
+#include "ProjectUserPrefs.h"
 #include "Project.h"
 
 using namespace rapidjson;
@@ -20,9 +25,11 @@ namespace ToolCore
 {
 
 Project::Project(Context* context) :
-    Object(context)
+    Object(context),
+    dirty_(false)
 {
-
+    userPrefs_ = new ProjectUserPrefs(context_);
+    buildSettings_ = new ProjectBuildSettings(context_);
 }
 
 Project::~Project()
@@ -79,11 +86,13 @@ void Project::LoadUserPrefs(const String& fullpath)
     }
     */
 
+    /*
     const Value::Member* last_build_path = document.FindMember("last_build_path");
     if (last_build_path && last_build_path->value.IsString())
     {
         lastBuildPath_ = last_build_path->value.GetString();
     }
+    */
 
     // probably will want to move this, it will trigger a save (which is guarded with load_)
     /*
@@ -95,55 +104,11 @@ void Project::LoadUserPrefs(const String& fullpath)
 
 void Project::SaveUserPrefs(const String& fullpath)
 {
-    //Editor* editor = GetSubsystem<Editor>();
-
-    FILE* file = fopen(fullpath.CString(), "w");
-
-    if (!file)
-        return;
-
-    rapidjson::FileStream s(file);
-    rapidjson::PrettyWriter<rapidjson::FileStream> writer(s);
 
-    writer.StartObject();
-    writer.String("version");
-    writer.Int(1);
-
-/*
-    writer.String("current_platform");
-
-    AEEditorPlatform platform = editor->GetCurrentPlatform();
-    if (platform == AE_PLATFORM_WINDOWS)
-        writer.String("Windows");
-    else if (platform == AE_PLATFORM_MAC)
-        writer.String("Mac");
-    else if (platform == AE_PLATFORM_HTML5)
-        writer.String("HTML5");
-    else if (platform == AE_PLATFORM_IOS)
-        writer.String("iOS");
-    else if (platform == AE_PLATFORM_ANDROID)
-        writer.String("Android");
-*/
-
-    writer.String("last_build_path");
-    writer.String(lastBuildPath_.CString());
-
-    writer.EndObject();
-
-    fclose(file);
 }
 
 void Project::SaveBuildSettings(const String& path)
 {
-    SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
-
-    jsonFile->CreateRoot();
-
-    SharedPtr<File> file(new File(context_, path, FILE_WRITE));
-
-    jsonFile->Save(*file, String("   "));
-
-    file->Close();
 
 }
 
@@ -154,93 +119,62 @@ bool Project::LoadBuildSettings(const String& path)
 
 void Project::AddPlatform(PlatformID platformID)
 {
+    if (ContainsPlatform(platformID))
+        return;
 
-}
+    dirty_ = true;
 
-void Project::RemovePlatform(PlatformID platformID)
-{
+    platforms_.Push(platformID);
 
 }
 
-void Project::Load(const String& fullpath)
+void Project::RemovePlatform(PlatformID platformID)
 {
-    projectFilePath_ = fullpath;
-
-    LoadUserPrefs(GetUserPrefsFullPath(fullpath));
-
-    rapidjson::Document document;
-
-    File jsonFile(context_, fullpath);
-
-    if (!jsonFile.IsOpen())
-    {
+    if (!ContainsPlatform(platformID))
         return;
-    }
 
-    String json;
-    jsonFile.ReadText(json);
+}
 
-    if (!json.Length())
+bool Project::ContainsPlatform(PlatformID platformID)
+{
+    for (List<PlatformID>::ConstIterator i = platforms_.Begin(); i != platforms_.End(); ++i)
     {
-        return;
+        if ((*i) == platformID)
+            return true;
     }
 
-    if (document.Parse<0>(json.CString()).HasParseError())
-    {
-        LOGERRORF("Could not parse Project JSON data from %s", fullpath.CString());
-        return;
-    }
+    return false;
 
-    const Value::Member* version = document.FindMember("version");
-    if (version && version->value.IsInt())
-    {
+}
 
-    }
+bool Project::Load(const String& fullpath)
+{
+    projectFilePath_ = fullpath;
+    SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
+    return pfile->Load(this);
+}
 
-    Value::Member* build_settings = document.FindMember("build_settings");
-    if (build_settings && build_settings->value.IsObject())
-    {
-        //BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
-        //buildSystem->LoadBuildSettings(build_settings);
-    }
+String Project::GetBuildSettingsFullPath()
+{
+    String path = GetPath(projectFilePath_);
+    String filename = GetFileName(projectFilePath_);
+    String buildSettingsPath = path + filename + ".buildsettings";
+    return buildSettingsPath;
 }
 
-String Project::GetUserPrefsFullPath(const String& projectPath)
+String Project::GetUserPrefsFullPath()
 {
-    String path = GetPath(projectPath);
-    String filename = GetFileName(projectPath);
-    String prefsPath = path + filename + ".atomic.userprefs";
+    String path = GetPath(projectFilePath_);
+    String filename = GetFileName(projectFilePath_);
+    String prefsPath = path + filename + ".userprefs";
     return prefsPath;
 }
 
 void Project::Save(const String& fullpath)
 {
-    if (fullpath.Length())
-        projectFilePath_ = fullpath;
-
-    String path = projectFilePath_;
-
-    SaveUserPrefs(GetUserPrefsFullPath(path));
-
-    FILE* file = fopen(path.CString(), "w");
-
-    if (!file)
-        return;
-
-    rapidjson::FileStream s(file);
-    rapidjson::PrettyWriter<rapidjson::FileStream> writer(s);
-
-    writer.StartObject();
-    writer.String("version");
-    writer.Int(1);
-
-    //BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
-    //buildSystem->SaveBuildSettings(writer);
-
-    writer.EndObject();
-
-    fclose(file);
-
+    SharedPtr<ProjectFile> pfile(new ProjectFile(context_));
+    pfile->Save(this);
+    dirty_ = false;
 }
 
 bool Project::IsComponentsDirOrFile(const String& fullPath)

+ 18 - 8
Source/ToolCore/Project/Project.h

@@ -8,8 +8,13 @@ using namespace Atomic;
 namespace ToolCore
 {
 
+class ProjectUserPrefs;
+class ProjectBuildSettings;
+
 class Project : public Object
 {
+    friend class ProjectFile;
+
     OBJECT(Project);
 
 public:
@@ -18,7 +23,7 @@ public:
     /// Destruct.
     virtual ~Project();
 
-    void Load(const String& fullpath);
+    bool Load(const String& fullpath);
     void Save(const String& fullpath = "");
 
     /// Paths
@@ -41,20 +46,24 @@ public:
     bool IsScriptsDirOrFile(const String& fullPath);
     bool IsModulesDirOrFile(const String& fullPath);
 
-    const String& GetLastBuildPath() { return lastBuildPath_; }
-    void SetLastBuildPath(const String& path) { lastBuildPath_ = path; }
-
     void SaveBuildSettings(const String& path);
     bool LoadBuildSettings(const String& path);
 
     void AddPlatform(PlatformID platformID);
+    bool ContainsPlatform(PlatformID platformID);
     void RemovePlatform(PlatformID platformID);
 
+    bool IsDirty() { return dirty_; }
+
+    ProjectBuildSettings* GetBuildSettings();
+
+    String GetUserPrefsFullPath();
+    String GetBuildSettingsFullPath();
+
 private:
 
     void LoadUserPrefs(const String& fullpath);
     void SaveUserPrefs(const String& fullpath);
-    String GetUserPrefsFullPath(const String& projectPath);
 
     String projectFilePath_;
     String resourcePath_;
@@ -63,11 +72,12 @@ private:
     String scriptsPath_;
     String modulesPath_;
 
-    String lastBuildPath_;
+    bool dirty_;
 
-    VariantMap buildSettings_;
+    SharedPtr<ProjectUserPrefs> userPrefs_;
+    SharedPtr<ProjectBuildSettings> buildSettings_;
 
-    List<SharedPtr<Platform>> platforms_;
+    List<PlatformID> platforms_;
 
 };
 

+ 42 - 0
Source/ToolCore/Project/ProjectBuildSettings.cpp

@@ -0,0 +1,42 @@
+
+#include <Atomic/IO/Log.h>
+#include <Atomic/IO/File.h>
+
+#include <Atomic/Resource/JSONFile.h>
+
+#include "ProjectBuildSettings.h"
+
+namespace ToolCore
+{
+
+ProjectBuildSettings::ProjectBuildSettings(Context* context) : Object(context)
+{
+
+}
+
+ProjectBuildSettings::~ProjectBuildSettings()
+{
+
+}
+
+bool ProjectBuildSettings::Load(const String& path)
+{
+    return true;
+}
+
+void ProjectBuildSettings::Save(const String& path)
+{
+    SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
+
+    jsonFile->CreateRoot();
+
+    SharedPtr<File> file(new File(context_, path, FILE_WRITE));
+
+    jsonFile->Save(*file, String("   "));
+
+    file->Close();
+
+}
+
+
+}

+ 26 - 0
Source/ToolCore/Project/ProjectBuildSettings.h

@@ -0,0 +1,26 @@
+
+#pragma once
+
+#include <Atomic/Core/Object.h>
+
+using namespace Atomic;
+
+namespace ToolCore
+{
+
+class ProjectBuildSettings : public Object
+{
+    OBJECT(ProjectBuildSettings);
+
+public:
+    /// Construct.
+    ProjectBuildSettings(Context* context);
+    /// Destruct.
+    virtual ~ProjectBuildSettings();
+
+    bool Load(const String& path);
+    void Save(const String& path);
+
+};
+
+}

+ 93 - 0
Source/ToolCore/Project/ProjectFile.cpp

@@ -0,0 +1,93 @@
+
+#include <Atomic/IO/Log.h>
+#include <Atomic/IO/File.h>
+
+#include <Atomic/Resource/JSONFile.h>
+
+#include "../ToolSystem.h"
+
+#include "Project.h"
+#include "ProjectFile.h"
+
+namespace ToolCore
+{
+
+ProjectFile::ProjectFile(Context* context) : Object(context)
+{
+
+}
+
+ProjectFile::~ProjectFile()
+{
+
+}
+
+void ProjectFile::Save(Project* project)
+{
+    project_ = project;
+    ToolSystem* tsystem = GetSubsystem<ToolSystem>();
+
+    String fullpath = project->GetProjectFilePath();
+
+    SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
+
+    JSONValue root = jsonFile->CreateRoot();
+    root.SetInt("version", PROJECTFILE_VERSION);
+
+    // platforms
+
+    JSONValue platforms = root.CreateChild("platforms", JSON_ARRAY);
+
+    for (List<PlatformID>::ConstIterator i = project_->platforms_.Begin(); i != project_->platforms_.End(); ++i)
+    {
+        Platform* platform = tsystem->GetPlatformByID(*i);
+        if (platform)
+        {
+            platforms.AddString(platform->GetName().ToLower());
+        }
+    }
+
+    // Save to file
+    SharedPtr<File> file(new File(context_, fullpath, FILE_WRITE));
+    jsonFile->Save(*file, String("   "));
+    file->Close();
+
+}
+
+bool ProjectFile::Load(Project* project)
+{
+    project_ = project;
+    ToolSystem* tsystem = GetSubsystem<ToolSystem>();
+
+    String fullpath = project->GetProjectFilePath();
+
+    SharedPtr<File> file(new File(context_, fullpath, FILE_READ));
+    SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
+
+    jsonFile->BeginLoad(*file);
+
+    JSONValue root = jsonFile->GetRoot();
+
+    int version = root.GetInt("version");
+
+    if (version != PROJECTFILE_VERSION)
+        return false;
+
+    JSONValue platforms = root.GetChild("platforms");
+    if (!platforms.IsArray())
+        return false;
+
+    for (unsigned i = 0; i < platforms.GetSize(); i++)
+    {
+        String jplatform = platforms.GetString(i);
+        Platform* platform = tsystem->GetPlatformByName(jplatform);
+        if (platform)
+            project_->AddPlatform(platform->GetPlatformID());
+    }
+
+    return true;
+
+}
+
+
+}

+ 34 - 0
Source/ToolCore/Project/ProjectFile.h

@@ -0,0 +1,34 @@
+
+#pragma once
+
+#include <Atomic/Core/Object.h>
+
+using namespace Atomic;
+
+namespace ToolCore
+{
+
+class Project;
+
+static const int PROJECTFILE_VERSION = 1;
+
+class ProjectFile : public Object
+{
+    OBJECT(ProjectFile);
+
+public:
+    /// Construct.
+    ProjectFile(Context* context);
+    /// Destruct.
+    virtual ~ProjectFile();
+
+    void Save(Project* project);
+    bool Load(Project* project);
+
+private:
+
+    SharedPtr<Project> project_;
+
+};
+
+}

+ 17 - 0
Source/ToolCore/Project/ProjectUserPrefs.cpp

@@ -0,0 +1,17 @@
+
+#include "ProjectUserPrefs.h"
+
+namespace ToolCore
+{
+
+ProjectUserPrefs::ProjectUserPrefs(Context* context) : Object(context)
+{
+
+}
+
+ProjectUserPrefs::~ProjectUserPrefs()
+{
+
+}
+
+}

+ 30 - 0
Source/ToolCore/Project/ProjectUserPrefs.h

@@ -0,0 +1,30 @@
+
+#pragma once
+
+#include <Atomic/Core/Object.h>
+
+using namespace Atomic;
+
+namespace ToolCore
+{
+
+class ProjectUserPrefs : public Object
+{
+    OBJECT(ProjectUserPrefs);
+
+public:
+    /// Construct.
+    ProjectUserPrefs(Context* context);
+    /// Destruct.
+    virtual ~ProjectUserPrefs();
+
+    const String& GetLastBuildPath() { return lastBuildPath_; }
+    void SetLastBuildPath(const String& path) { lastBuildPath_ = path; }
+
+private:
+
+    String lastBuildPath_;
+
+};
+
+}

+ 1 - 3
Source/ToolCore/ToolSystem.cpp

@@ -46,10 +46,8 @@ bool ToolSystem::LoadProject(const String& fullpath)
 
     project_ = new Project(context_);
     project_->SetResourcePath(resourcePath);
-    project_->Load(fullpath);
-
-    return true;
 
+    return project_->Load(fullpath);
 }
 
 void ToolSystem::SetCurrentPlatform(PlatformID platform)