blob: 6a677f8baad7af37a78c4258bf737bd207d05966 [file] [log] [blame]
agsantos1e7736c2020-10-28 14:39:13 -04001#!/usr/bin/env python3
2#
Sébastien Blincb783e32021-02-12 11:34:10 -05003# Copyright (C) 2020-2021 Savoir-faire Linux Inc.
agsantos1e7736c2020-10-28 14:39:13 -04004#
5# Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20# Creates packaging targets for a distribution and architecture.
21# This helps reduce the length of the top Makefile.
22#
23
24from userProfile import UserInfos
25from sdkConstants import *
26import os
27import json
28import shutil
29from termcolor import colored, cprint
30
31
32class PluginStructure(UserInfos):
33 def __init__(self, action):
34 UserInfos.__init__(self, action)
35 self.create(action)
36 self.createFolderStructure()
37
38 def delete(self):
39 if (os.path.exists(self.pluginDirectory)
40 and os.path.isdir(self.pluginDirectory)):
41 shutil.rmtree(self.pluginDirectory)
42
43 def reInitialize(self, pluginName):
44 self.pluginName = pluginName
45 self.pluginDirectory = f"./../{self.pluginName}"
46 self.manifestFile = f"{self.pluginDirectory}/manifest.json"
47 self.pluginDataDirectory = f"{self.pluginDirectory}/data"
agsantosd00bf412021-01-26 13:43:33 -050048 self.pluginIcon = f"{self.pluginDataDirectory}/icon.svg"
agsantos1e7736c2020-10-28 14:39:13 -040049 self.preferencesFile = f"{self.pluginDataDirectory}/preferences.json"
50 self.packageFile = f"{self.pluginDirectory}/package.json"
51 self.cmakelistsFile = f"{self.pluginDirectory}/CMakeLists.txt"
52 self.buildFile = f"{self.pluginDirectory}/build.sh"
53
54 def create(self, action):
55 plugins = os.listdir("../")
56 print(colored("\nLeave Empty or Press 'q' to quit this option.", "yellow"))
57 if (action == ACTIONS["CREATE"]):
58 print("\nNow, you need to tell how you want yout plugin to be called.")
59 pluginName = "SDK"
60 while (pluginName in plugins):
61 pluginName = input("\nChoose a cool name for your plugin: ")
62 if (pluginName == 'q' or not pluginName):
63 self.reInitialize('')
64 break
65 if (pluginName in plugins):
66 print(colored("This name is invalid!", "red"))
67 else:
68 self.reInitialize(pattern.sub('', pluginName))
69 if (self.pluginName):
70 coloredTitle = colored(self.pluginName, "green")
71 print(f"\nNice! Your {coloredTitle} will be awesome!")
72
73 elif (action == ACTIONS["DELETE"] or action == ACTIONS["DELETE_PREFERENCES"] \
74 or action == ACTIONS["DELETE_MANIFEST"]):
75 pluginName = ""
76 while (pluginName not in plugins or pluginName in FORBIDDEN_NAMES):
77 pluginName = input("\nPlugin to be modified: ")
78 if (pluginName == 'q' or not pluginName):
79 self.reInitialize('')
80 break
81 if (pluginName not in plugins or pluginName in FORBIDDEN_NAMES):
82 print(colored("This name is invalid!", "red"))
83 else:
84 self.reInitialize(pattern.sub('', pluginName))
85
86 elif (action == ACTIONS["CREATE/MODIFY_MANIFEST"] or action == ACTIONS["CREATE_PREFERENCES"] \
87 or action == ACTIONS["CREATE_FUNCTIONALITY"] or action == ACTIONS["CREATE_PACKAGE_JSON"] \
88 or action == ACTIONS["CREATE_MAIN"] or action == ACTIONS["CREATE_BUILD_FILES"] \
89 or action == ACTIONS["ASSEMBLE"] or action == ACTIONS["PREASSEMBLE"] \
90 or action == ACTIONS["BUILD"]):
91 pluginName = ""
92 while (pluginName in FORBIDDEN_NAMES):
93 pluginName = input("\nPlugin to be modified or created: ")
94 if (pluginName == 'q' or not pluginName):
95 self.reInitialize('')
96 break
97 if (pluginName in FORBIDDEN_NAMES):
98 print(colored("This name is invalid!", "red"))
99 else:
100 self.reInitialize(pattern.sub('', pluginName))
101
102 def createFolderStructure(self):
103 if (self.pluginName):
104 if (not os.path.exists(self.pluginDirectory)):
105 os.mkdir(self.pluginDirectory)
106 if (not os.path.exists(self.pluginDataDirectory)):
107 os.mkdir(self.pluginDataDirectory)
108 if (not os.path.exists(self.pluginIcon)):
agsantosd00bf412021-01-26 13:43:33 -0500109 shutil.copyfile("./Templates/icon.svg", self.pluginIcon)
agsantos1e7736c2020-10-28 14:39:13 -0400110
111 def saveManifest(self, manifestTxt):
112 with open(self.manifestFile, 'w') as f:
113 json.dump(manifestTxt, f, indent=4)
114 f.close()
115
116 def deleteManifest(self):
117 if (os.path.exists(self.manifestFile)):
118 os.remove(self.manifestFile)
119
120 def isManifest(self):
121 return os.path.exists(self.manifestFile)
122
123 def isMainDirectory(self):
124 return os.path.exists(self.pluginDirectory)