1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-licence
14    Privacy Policy: www.juce.com/juce-privacy-policy
15 
16    Or: You may also use this code under the terms of the GPL v3 (see
17    www.gnu.org/licenses).
18 
19    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21    DISCLAIMED.
22 
23   ==============================================================================
24 */
25 
26 #pragma once
27 
28 #include "../Application/jucer_Headers.h"
29 #include "jucer_ResourceFile.h"
30 #include "../Project/Modules/jucer_Modules.h"
31 #include "jucer_ProjectExporter.h"
32 
33 //==============================================================================
34 class ProjectSaver
35 {
36 public:
37     ProjectSaver (Project& projectToSave);
38 
39     Result save (ProjectExporter* exporterToSave = nullptr);
40     Result saveResourcesOnly();
41     void saveBasicProjectItems (const OwnedArray<LibraryModule>& modules, const String& appConfigUserContent);
42     Result saveContentNeededForLiveBuild();
43 
getProject()44     Project& getProject()  { return project; }
45 
46     Project::Item addFileToGeneratedGroup (const File& file);
47     bool copyFolder (const File& source, const File& dest);
48 
getJuceCodeGroupName()49     static String getJuceCodeGroupName()  { return "JUCE Library Code"; }
50 
51 private:
52     //==============================================================================
53     struct SaveThreadWithProgressWindow  : public ThreadWithProgressWindow
54     {
55     public:
SaveThreadWithProgressWindowSaveThreadWithProgressWindow56         SaveThreadWithProgressWindow (ProjectSaver& ps, ProjectExporter* exporterToSave)
57             : ThreadWithProgressWindow ("Saving...", true, false),
58               saver (ps),
59               specifiedExporterToSave (exporterToSave)
60         {}
61 
runSaveThreadWithProgressWindow62         void run() override
63         {
64             setProgress (-1);
65             result = saver.saveProject (specifiedExporterToSave);
66         }
67 
68         ProjectSaver& saver;
69         Result result = Result::ok();
70         ProjectExporter* specifiedExporterToSave;
71 
72         JUCE_DECLARE_NON_COPYABLE (SaveThreadWithProgressWindow)
73     };
74 
75     //==============================================================================
76     Project::Item saveGeneratedFile (const String& filePath, const MemoryOutputStream& newData);
77     bool replaceFileIfDifferent (const File& f, const MemoryOutputStream& newData);
78     bool deleteUnwantedFilesIn (const File& parent);
79 
80     void addError (const String& message);
81 
82     File getAppConfigFile() const;
83     File getPluginDefinesFile() const;
84 
85     String loadUserContentFromAppConfig() const;
86     String getAudioPluginDefines() const;
87     OwnedArray<LibraryModule> getModules();
88 
89     Result saveProject (ProjectExporter* specifiedExporterToSave);
90 
91     template <typename WriterCallback>
92     void writeOrRemoveGeneratedFile (const String& name, WriterCallback&& writerCallback);
93 
94     void writePluginDefines (MemoryOutputStream& outStream) const;
95     void writePluginDefines();
96     void writeAppConfigFile (const OwnedArray<LibraryModule>& modules, const String& userContent);
97 
98     void writeProjectFile();
99     void writeAppConfig (MemoryOutputStream& outStream, const OwnedArray<LibraryModule>& modules, const String& userContent);
100     void writeAppHeader (MemoryOutputStream& outStream, const OwnedArray<LibraryModule>& modules);
101     void writeAppHeader (const OwnedArray<LibraryModule>& modules);
102     void writeModuleCppWrappers (const OwnedArray<LibraryModule>& modules);
103     void writeBinaryDataFiles();
104     void writeReadmeFile();
105     void writePluginCharacteristicsFile();
106     void writeUnityScriptFile();
107     void writeProjects (const OwnedArray<LibraryModule>&, ProjectExporter*);
108     void runPostExportScript();
109     void saveExporter (ProjectExporter& exporter, const OwnedArray<LibraryModule>& modules);
110 
111     //==============================================================================
112     Project& project;
113 
114     File generatedCodeFolder;
115     Project::Item generatedFilesGroup;
116     SortedSet<File> filesCreated;
117     String projectLineFeed;
118 
119     CriticalSection errorLock;
120     StringArray errors;
121 
122     bool hasBinaryData = false;
123 
124     //==============================================================================
125     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProjectSaver)
126 };
127