@@ -11,14 +11,8 @@ import (
1111 "github.com/AmadlaOrg/LibraryUtils/pointer"
1212)
1313
14- // IDesktop 🧩 Is the interface for the NewDesktopService.
15- type IDesktop interface {
16- Build (desktop * Desktop ) (IDesktop , error )
17- Save (dirPath string ) error
18- }
19-
20- // SDesktop 🏛️ Is the main structure for the NewDesktopService.
21- type SDesktop struct {
14+ // Builder 🏛️ Is the main structure for the NewDesktopService.
15+ type Builder struct {
2216 // 📇 appName - Is the of the application (normally all lowercase).
2317 appName string
2418
3125 osWriteFile = os .WriteFile
3226)
3327
34- // Build generates the content for the `.desktop` file format by processing each group in the provided `Desktop` object.
28+ // NewBuilder creates a new instance of DesktopBuilder.
29+ //
30+ // Params:
31+ // - 📇 appName - Is the of the application (normally all lowercase).
32+ func NewBuilder (appName string ) Generator {
33+ return & Builder {
34+ appName : appName ,
35+ }
36+ }
37+
38+ // Generate generates the content for the `.desktop` file format by processing each group in the provided `Desktop` object.
3539// It builds sections and properties based on the `.desktop` specification.
3640//
3741// Params:
@@ -40,148 +44,97 @@ var (
4044// Returns:
4145// - IDesktop: The constructed desktop representation.
4246// - 🚨 error: Returns an error if any required property is missing or incorrectly formatted.
43- func (service * SDesktop ) Build (desktop * Desktop ) (IDesktop , error ) {
47+ func (b * Builder ) Generate (desktop * Desktop ) (Generator , error ) {
4448 var (
4549 groupNames []string
4650 )
4751
4852 groups := * desktop .Groups
4953
5054 for _ , group := range groups {
51- //
52- // Section
53- //
54- var thisSectionName string
55+ var thisGroupName string
5556
5657 if group .Title == nil ||
5758 * group .Title == "" ||
5859 * group .Title == defaultDesktopSectionName {
59- thisSectionName = fmt .Sprintf ("[%s]\n " , defaultDesktopSectionName )
60+ thisGroupName = fmt .Sprintf ("[%s]\n " , defaultDesktopSectionName )
6061 } else {
61- thisSectionName = fmt .Sprintf ("[%s]\n " , * group .Title )
62+ thisGroupName = fmt .Sprintf ("[%s]\n " , * group .Title )
6263 }
6364
64- service .builder .WriteString (thisSectionName )
65- groupNames = append (groupNames , thisSectionName )
65+ b .builder .WriteString (thisGroupName )
66+ groupNames = append (groupNames , thisGroupName )
6667
67- //
68- // Name
69- //
70- err := service .processContent ("Name" , group .Names , true )
68+ err := b .processContent ("Name" , group .Names , true )
7169 if err != nil {
7270 return nil , err
7371 }
74-
75- //
76- // GenericNames
77- //
78- _ = service .processContent ("GenericName" , group .GenericNames , false )
79-
80- //
81- // Comments
82- //
83- _ = service .processContent ("Comment" , group .Comments , false )
84-
85- //
86- // Keywords
87- //
88- _ = service .processContent ("Keywords" , group .Keywords , false )
89-
90- //
91- // Version
92- //
93- service .processPropertyDefault ("Version" , group .Version , "1.0" )
94-
95- //
96- // X-AppVersion
97- //
98- err = service .processRequiredProperty ("X-AppVersion" , group .XAppVersion )
72+ _ = b .processContent ("GenericName" , group .GenericNames , false )
73+ _ = b .processContent ("Comment" , group .Comments , false )
74+ _ = b .processContent ("Keywords" , group .Keywords , false )
75+ b .processPropertyDefault ("Version" , group .Version , "1.0" )
76+ err = b .processRequiredProperty ("X-AppVersion" , group .XAppVersion )
9977 if err != nil {
10078 return nil , err
10179 }
80+ b .processNotRequiredProperty ("Icon" , group .Icon )
81+ _ = b .processList ("Categories" , group .Categories , false )
82+ _ = b .processCommaList ("X-KDE-Protocols" , group .XKDEProtocols , false )
83+ b .processNotRequiredProperty ("Encoding" , group .Encoding )
10284
103- //
104- // Icon
105- //
106- service .processNotRequiredProperty ("Icon" , group .Icon )
107-
108- //
109- // Categories
110- //
111- _ = service .processList ("Categories" , group .Categories , false )
112-
113- //
114- // x-kde-protocols
115- //
116- _ = service .processCommaList ("X-KDE-Protocols" , group .XKDEProtocols , false )
117-
118- //
119- // Encoding
120- //
121- service .processNotRequiredProperty ("Encoding" , group .Encoding )
122-
123- //
124- // Terminal
125- //
126- service .processPropertyDefault (
85+ b .processPropertyDefault (
12786 "Terminal" ,
12887 pointer .ToPtr (strconv .FormatBool (group .Terminal )),
12988 "true" )
13089
131- //
132- // Type
133- //
13490 if group .Type == nil || * group .Type == "" {
13591 group .Type = (* Type )(pointer .ToPtr ("Application" ))
13692 }
13793
138- service .builder .WriteString (fmt .Sprintf ("Type=%s\n " , string (* group .Type )))
94+ b .builder .WriteString (fmt .Sprintf ("Type=%s\n " , string (* group .Type )))
13995 if * group .Type == ApplicationType {
14096 //
14197 // Exec
14298 //
143- err = service .processRequiredProperty ("Exec" , & group .Exec )
99+ err = b .processRequiredProperty ("Exec" , & group .Exec )
144100 if err != nil {
145101 return nil , err
146102 }
147103 } else if * group .Type == LinkType {
148104 //
149105 // Url
150106 //
151- err = service .processRequiredProperty ("Url" , & group .URL )
107+ err = b .processRequiredProperty ("Url" , & group .URL )
152108 if err != nil {
153109 return nil , err
154110 }
155111 }
156112
157- //
158- // MimeType
159- //
160- _ = service .processList ("MimeType" , group .MimeType , false )
113+ _ = b .processList ("MimeType" , group .MimeType , false )
161114 }
162115
163- return service , nil
116+ return b , nil
164117}
165118
166- // Save writes the generated `.desktop` content to a file in the specified directory.
119+ // WriteToFile writes the generated `.desktop` content to a file in the specified directory.
167120// It ensures the directory exists before writing.
168121//
169122// Params:
170123// - 📁 dirPath: The directory where the `.desktop` file should be saved.
171124//
172125// Returns:
173126// - 🚨 error: Returns an error if the directory cannot be created or if writing to the file fails.
174- func (service * SDesktop ) Save (dirPath string ) error {
127+ func (b * Builder ) WriteToFile (dirPath string ) error {
175128 err := osMkdirAll (dirPath , 0755 )
176129 if err != nil {
177130 return fmt .Errorf ("failed to create directory: %w" , err )
178131 }
179132
180133 // Construct the file path
181- filePath := filepath .Join (dirPath , fmt .Sprintf ("%s.desktop" , service .appName ))
134+ filePath := filepath .Join (dirPath , fmt .Sprintf ("%s.desktop" , b .appName ))
182135
183136 // Get the .desktop content
184- desktopContent := service .builder .String ()
137+ desktopContent := b .builder .String ()
185138
186139 // Write to the file
187140 err = osWriteFile (filePath , []byte (desktopContent ), 0644 )
@@ -202,7 +155,7 @@ func (service *SDesktop) Save(dirPath string) error {
202155//
203156// Returns:
204157// - 🚨 error: Returns an error if isNeeded is true and the property is not set properly.
205- func (service * SDesktop ) processContent (propertyName string , contentValues * []Content , isNeeded bool ) error {
158+ func (b * Builder ) processContent (propertyName string , contentValues * []Content , isNeeded bool ) error {
206159 var (
207160 isSet = false
208161 isSetWithLang = false
@@ -211,10 +164,10 @@ func (service *SDesktop) processContent(propertyName string, contentValues *[]Co
211164 for _ , value := range * contentValues {
212165 if value .Language == nil {
213166 isSet = true
214- service .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , value .Value ))
167+ b .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , value .Value ))
215168 } else {
216169 isSetWithLang = true
217- service .builder .WriteString (fmt .Sprintf ("%s[%s]=%s\n " , propertyName , * value .Language , value .Value ))
170+ b .builder .WriteString (fmt .Sprintf ("%s[%s]=%s\n " , propertyName , * value .Language , value .Value ))
218171 }
219172 }
220173
@@ -239,11 +192,11 @@ func (service *SDesktop) processContent(propertyName string, contentValues *[]Co
239192// - 🏠 propertyName: The name of the property being processed.
240193// - 💎 contentValue: A pointer to the string value of the property (maybe nil).
241194// - ⚠️ defaultValue: The default value to use if contentValue is nil or empty.
242- func (service * SDesktop ) processPropertyDefault (propertyName string , contentValue * string , defaultValue string ) {
195+ func (b * Builder ) processPropertyDefault (propertyName string , contentValue * string , defaultValue string ) {
243196 if contentValue == nil || * contentValue == "" {
244- service .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , defaultValue ))
197+ b .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , defaultValue ))
245198 } else {
246- service .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , * contentValue ))
199+ b .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , * contentValue ))
247200 }
248201}
249202
@@ -256,9 +209,9 @@ func (service *SDesktop) processPropertyDefault(propertyName string, contentValu
256209//
257210// Returns:
258211// - 🚨 error: Returns an error if contentValue is nil or empty.
259- func (service * SDesktop ) processRequiredProperty (propertyName string , contentValue * string ) error {
212+ func (b * Builder ) processRequiredProperty (propertyName string , contentValue * string ) error {
260213 if contentValue != nil && * contentValue != "" {
261- service .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , * contentValue ))
214+ b .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , * contentValue ))
262215 return nil
263216 } else {
264217 return fmt .Errorf ("the property %s is required" , propertyName )
@@ -272,9 +225,9 @@ func (service *SDesktop) processRequiredProperty(propertyName string, contentVal
272225// Params:
273226// - 🏠 propertyName: The name of the property being processed.
274227// - 💎 contentValue: A pointer to the string value of the property (maybe nil).
275- func (service * SDesktop ) processNotRequiredProperty (propertyName string , contentValue * string ) {
228+ func (b * Builder ) processNotRequiredProperty (propertyName string , contentValue * string ) {
276229 if contentValue != nil && * contentValue != "" {
277- service .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , * contentValue ))
230+ b .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , * contentValue ))
278231 }
279232}
280233
@@ -288,19 +241,15 @@ func (service *SDesktop) processNotRequiredProperty(propertyName string, content
288241//
289242// Returns:
290243// - 🚨 error: Returns an error if `isNeeded` is true and the list is empty.
291- func (service * SDesktop ) processList (propertyName string , items * []List , isNeeded bool ) error {
244+ func (b * Builder ) processList (propertyName string , items * []List , isNeeded bool ) error {
292245 if isNeeded && (items == nil || len (* items ) == 0 ) {
293246 return fmt .Errorf ("the property %s is empty" , propertyName )
294247 }
295248
296249 if items != nil && len (* items ) > 0 {
297- // Convert []List to []string
298- stringItems := make ([]string , len (* items ))
299- for i , item := range * items {
300- stringItems [i ] = string (item ) // Convert List to string
301- }
302-
303- service .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , strings .Join (stringItems , ";" )))
250+ processedString := fmt .Sprintf (
251+ "%s=%s\n " , propertyName , strings .Join (convertListToStrings (* items ), ";" ))
252+ b .builder .WriteString (processedString )
304253 }
305254
306255 return nil
@@ -316,19 +265,15 @@ func (service *SDesktop) processList(propertyName string, items *[]List, isNeede
316265//
317266// Returns:
318267// - 🚨 error: Returns an error if `isNeeded` is true and the list is empty.
319- func (service * SDesktop ) processCommaList (propertyName string , items * []CommaList , isNeeded bool ) error {
268+ func (b * Builder ) processCommaList (propertyName string , items * []CommaList , isNeeded bool ) error {
320269 if isNeeded && (items == nil || len (* items ) == 0 ) {
321270 return fmt .Errorf ("the property %s is empty" , propertyName )
322271 }
323272
324273 if items != nil && len (* items ) > 0 {
325- // Convert []List to []string
326- stringItems := make ([]string , len (* items ))
327- for i , item := range * items {
328- stringItems [i ] = string (item ) // Convert List to string
329- }
330-
331- service .builder .WriteString (fmt .Sprintf ("%s=%s\n " , propertyName , strings .Join (stringItems , "," )))
274+ processedString := fmt .Sprintf (
275+ "%s=%s\n " , propertyName , strings .Join (convertListToStrings (* items ), "," ))
276+ b .builder .WriteString (processedString )
332277 }
333278
334279 return nil
0 commit comments