@@ -31,140 +31,146 @@ var (
3131 osWriteFile = os .WriteFile
3232)
3333
34- // Build generates the content for the `.desktop` file format
34+ // Build generates the content for the `.desktop` file format by processing each group in the provided `Desktop` object.
35+ // It builds sections and properties based on the `.desktop` specification.
3536//
3637// Params:
37- // - 🖥️ desktop:
38- // - 📍 location:
38+ // - 🖥️ desktop: A pointer to the Desktop struct containing groups and their properties.
39+ //
40+ // Returns:
41+ // - IDesktop: The constructed desktop representation.
42+ // - 🚨 error: Returns an error if any required property is missing or incorrectly formatted.
3943func (service * SDesktop ) Build (desktop * Desktop ) (IDesktop , error ) {
4044 var (
41- sectionNames []string
45+ groupNames []string
4246 )
4347
44- sections := * desktop .Sections
48+ groups := * desktop .Groups
4549
46- for _ , section := range sections {
50+ for _ , group := range groups {
4751 //
4852 // Section
4953 //
5054 var thisSectionName string
5155
52- if section .Title == nil ||
53- * section .Title == "" ||
54- * section .Title == defaultDesktopSectionName {
56+ if group .Title == nil ||
57+ * group .Title == "" ||
58+ * group .Title == defaultDesktopSectionName {
5559 thisSectionName = fmt .Sprintf ("[%s]\n " , defaultDesktopSectionName )
5660 } else {
57- thisSectionName = fmt .Sprintf ("[%s]\n " , * section .Title )
61+ thisSectionName = fmt .Sprintf ("[%s]\n " , * group .Title )
5862 }
5963
6064 service .builder .WriteString (thisSectionName )
61- sectionNames = append (sectionNames , thisSectionName )
65+ groupNames = append (groupNames , thisSectionName )
6266
6367 //
6468 // Name
6569 //
66- err := service .processContent ("Name" , section .Names , true )
70+ err := service .processContent ("Name" , group .Names , true )
6771 if err != nil {
6872 return nil , err
6973 }
7074
7175 //
7276 // GenericNames
7377 //
74- _ = service .processContent ("GenericName" , section .GenericNames , false )
78+ _ = service .processContent ("GenericName" , group .GenericNames , false )
7579
7680 //
7781 // Comments
7882 //
79- _ = service .processContent ("Comment" , section .Comments , false )
83+ _ = service .processContent ("Comment" , group .Comments , false )
8084
8185 //
8286 // Keywords
8387 //
84- _ = service .processContent ("Keywords" , section .Keywords , false )
88+ _ = service .processContent ("Keywords" , group .Keywords , false )
8589
8690 //
8791 // Version
8892 //
89- service .processPropertyDefault ("Version" , section .Version , "1.0" )
93+ service .processPropertyDefault ("Version" , group .Version , "1.0" )
9094
9195 //
9296 // X-AppVersion
9397 //
94- err = service .processRequiredProperty ("X-AppVersion" , section .XAppVersion )
98+ err = service .processRequiredProperty ("X-AppVersion" , group .XAppVersion )
9599 if err != nil {
96100 return nil , err
97101 }
98102
99103 //
100104 // Icon
101105 //
102- service .processNotRequiredProperty ("Icon" , section .Icon )
106+ service .processNotRequiredProperty ("Icon" , group .Icon )
103107
104108 //
105109 // Categories
106110 //
107- _ = service .processList ("Categories" , section .Categories , false )
111+ _ = service .processList ("Categories" , group .Categories , false )
108112
109113 //
110114 // x-kde-protocols
111115 //
112- _ = service .processCommaList ("X-KDE-Protocols" , section .XKDEProtocols , false )
116+ _ = service .processCommaList ("X-KDE-Protocols" , group .XKDEProtocols , false )
113117
114118 //
115119 // Encoding
116120 //
117- service .processNotRequiredProperty ("Encoding" , section .Encoding )
121+ service .processNotRequiredProperty ("Encoding" , group .Encoding )
118122
119123 //
120124 // Terminal
121125 //
122126 service .processPropertyDefault (
123127 "Terminal" ,
124- pointer .ToPtr (strconv .FormatBool (section .Terminal )),
128+ pointer .ToPtr (strconv .FormatBool (group .Terminal )),
125129 "true" )
126130
127131 //
128132 // Type
129133 //
130- if section .Type == nil || * section .Type == "" {
131- section .Type = (* Type )(pointer .ToPtr ("Application" ))
134+ if group .Type == nil || * group .Type == "" {
135+ group .Type = (* Type )(pointer .ToPtr ("Application" ))
132136 }
133137
134- service .builder .WriteString (fmt .Sprintf ("Type=%s\n " , string (* section .Type )))
135- if * section .Type == ApplicationType {
138+ service .builder .WriteString (fmt .Sprintf ("Type=%s\n " , string (* group .Type )))
139+ if * group .Type == ApplicationType {
136140 //
137141 // Exec
138142 //
139- err = service .processRequiredProperty ("Exec" , & section .Exec )
143+ err = service .processRequiredProperty ("Exec" , & group .Exec )
140144 if err != nil {
141145 return nil , err
142146 }
143- } else if * section .Type == LinkType {
147+ } else if * group .Type == LinkType {
144148 //
145149 // Url
146150 //
147- err = service .processRequiredProperty ("Url" , & section .URL )
151+ err = service .processRequiredProperty ("Url" , & group .URL )
148152 if err != nil {
149153 return nil , err
150154 }
151- } else if * section .Type == DirectoryType {
152- // TODO:
153155 }
154156
155157 //
156158 // MimeType
157159 //
158- _ = service .processList ("MimeType" , section .MimeType , false )
160+ _ = service .processList ("MimeType" , group .MimeType , false )
159161 }
160162
161163 return service , nil
162164}
163165
164- // Save to a `.desktop` file.
166+ // Save writes the generated `.desktop` content to a file in the specified directory.
167+ // It ensures the directory exists before writing.
165168//
166169// Params:
167- // - dirPath:
170+ // - 📁 dirPath: The directory where the `.desktop` file should be saved.
171+ //
172+ // Returns:
173+ // - 🚨 error: Returns an error if the directory cannot be created or if writing to the file fails.
168174func (service * SDesktop ) Save (dirPath string ) error {
169175 err := osMkdirAll (dirPath , 0755 )
170176 if err != nil {
0 commit comments