Skip to content

Commit 141ae29

Browse files
committed
Update ConfiginfoBuilder.cs
1 parent 34cd2bd commit 141ae29

1 file changed

Lines changed: 2 additions & 260 deletions

File tree

src/c#/GeneralUpdate.Common/Shared/Object/ConfiginfoBuilder.cs

Lines changed: 2 additions & 260 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Runtime.InteropServices;
53

64
namespace GeneralUpdate.Common.Shared.Object
75
{
@@ -16,14 +14,14 @@ public class ConfiginfoBuilder
1614
/// <summary>
1715
/// Default blacklisted file format extensions that are automatically excluded from updates.
1816
/// </summary>
19-
public static readonly string[] DefaultBlackFormats = { ".log", ".tmp" };
17+
public static readonly string[] DefaultBlackFormats;
2018

2119
private readonly string _updateUrl;
2220
private readonly string _token;
2321
private readonly string _scheme;
2422

2523
// Configurable default values
26-
private string _appName = "App.exe";
24+
private string _appName = "Update.exe";
2725
private string _mainAppName = "App.exe";
2826
private string _clientVersion = "1.0.0";
2927
private string _upgradeClientVersion = "1.0.0";
@@ -90,268 +88,12 @@ public ConfiginfoBuilder(string updateUrl, string token, string scheme)
9088
/// </summary>
9189
private void InitializePlatformDefaults()
9290
{
93-
// Try to extract application metadata from project file
94-
var projectMetadata = TryExtractProjectMetadata();
95-
96-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
97-
{
98-
InitializeWindowsDefaults(projectMetadata);
99-
}
100-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
101-
{
102-
InitializeLinuxDefaults(projectMetadata);
103-
}
104-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
105-
{
106-
InitializeMacOSDefaults(projectMetadata);
107-
}
108-
else
109-
{
110-
// Fallback to Linux-style defaults for other Unix-like platforms
111-
InitializeLinuxDefaults(projectMetadata);
112-
}
113-
11491
// Initialize common defaults
11592
_blackFiles = new List<string>();
11693
_blackFormats = new List<string>(DefaultBlackFormats);
11794
_skipDirectorys = new List<string>();
11895
}
11996

120-
/// <summary>
121-
/// Represents metadata extracted from a project file.
122-
/// </summary>
123-
private class ProjectMetadata
124-
{
125-
public string AppName { get; set; }
126-
public string Version { get; set; }
127-
public string Company { get; set; }
128-
public string Authors { get; set; }
129-
}
130-
131-
/// <summary>
132-
/// Attempts to extract application metadata from the project file (csproj).
133-
/// This implements zero-configuration by reading from the host program's project metadata.
134-
/// </summary>
135-
/// <returns>ProjectMetadata with extracted values, or default values if extraction fails.</returns>
136-
private ProjectMetadata TryExtractProjectMetadata()
137-
{
138-
var metadata = new ProjectMetadata();
139-
140-
try
141-
{
142-
// Start from the application's base directory
143-
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
144-
string currentDirectory = baseDirectory;
145-
146-
// Search up to 3 levels up for a csproj file
147-
for (int i = 0; i < 3; i++)
148-
{
149-
if (string.IsNullOrEmpty(currentDirectory))
150-
break;
151-
152-
// Look for csproj files in the current directory
153-
string[] csprojFiles = Directory.GetFiles(currentDirectory, "*.csproj", SearchOption.TopDirectoryOnly);
154-
155-
if (csprojFiles.Length > 0)
156-
{
157-
// Use the first csproj file found
158-
string csprojPath = csprojFiles[0];
159-
ExtractMetadataFromCsproj(csprojPath, metadata);
160-
return metadata;
161-
}
162-
163-
// Move up one directory
164-
DirectoryInfo parentDir = Directory.GetParent(currentDirectory);
165-
currentDirectory = parentDir?.FullName;
166-
}
167-
}
168-
catch
169-
{
170-
// If extraction fails for any reason, return empty metadata
171-
}
172-
173-
return metadata;
174-
}
175-
176-
/// <summary>
177-
/// Extracts metadata fields from a csproj file.
178-
/// </summary>
179-
/// <param name="csprojPath">Path to the csproj file.</param>
180-
/// <param name="metadata">ProjectMetadata object to populate.</param>
181-
private void ExtractMetadataFromCsproj(string csprojPath, ProjectMetadata metadata)
182-
{
183-
try
184-
{
185-
string content = File.ReadAllText(csprojPath);
186-
187-
// Extract <AssemblyName>
188-
metadata.AppName = ExtractXmlElement(content, "AssemblyName");
189-
190-
// If AssemblyName is not specified, use the csproj file name (without extension)
191-
if (string.IsNullOrEmpty(metadata.AppName))
192-
{
193-
metadata.AppName = Path.GetFileNameWithoutExtension(csprojPath);
194-
}
195-
196-
// Extract <Version>
197-
metadata.Version = ExtractXmlElement(content, "Version");
198-
199-
// Extract <Company>
200-
metadata.Company = ExtractXmlElement(content, "Company");
201-
202-
// Extract <Authors>
203-
metadata.Authors = ExtractXmlElement(content, "Authors");
204-
}
205-
catch
206-
{
207-
// If parsing fails, use the file name as fallback for app name
208-
if (string.IsNullOrEmpty(metadata.AppName))
209-
{
210-
metadata.AppName = Path.GetFileNameWithoutExtension(csprojPath);
211-
}
212-
}
213-
}
214-
215-
/// <summary>
216-
/// Extracts the content of an XML element from a string.
217-
/// </summary>
218-
/// <param name="content">The XML/csproj content.</param>
219-
/// <param name="elementName">The name of the XML element to extract.</param>
220-
/// <returns>The element content, or null if not found.</returns>
221-
private string ExtractXmlElement(string content, string elementName)
222-
{
223-
string openTag = $"<{elementName}>";
224-
string closeTag = $"</{elementName}>";
225-
226-
int startIndex = content.IndexOf(openTag, StringComparison.OrdinalIgnoreCase);
227-
if (startIndex >= 0)
228-
{
229-
startIndex += openTag.Length;
230-
int endIndex = content.IndexOf(closeTag, startIndex, StringComparison.OrdinalIgnoreCase);
231-
if (endIndex > startIndex)
232-
{
233-
return content.Substring(startIndex, endIndex - startIndex).Trim();
234-
}
235-
}
236-
237-
return null;
238-
}
239-
240-
/// <summary>
241-
/// Initializes default values specific to Windows platform.
242-
/// </summary>
243-
/// <param name="metadata">The project metadata extracted from csproj file.</param>
244-
private void InitializeWindowsDefaults(ProjectMetadata metadata)
245-
{
246-
// Use the current application's base directory (no admin privileges required)
247-
// This extracts the path from the host program's runtime location
248-
_installPath = AppDomain.CurrentDomain.BaseDirectory;
249-
250-
// Windows uses backslash as path separator (handled automatically by Path.Combine)
251-
// Set Windows-specific executable names - prefer detected name from project file
252-
string appNameBase = metadata?.AppName ?? "App";
253-
_appName = appNameBase + ".exe";
254-
_mainAppName = appNameBase + ".exe";
255-
256-
// Set version if available from project metadata
257-
if (!string.IsNullOrEmpty(metadata?.Version))
258-
{
259-
_clientVersion = metadata.Version;
260-
_upgradeClientVersion = metadata.Version;
261-
}
262-
263-
// Set product ID from company/authors if available
264-
if (!string.IsNullOrEmpty(metadata?.Company))
265-
{
266-
_productId = metadata.Company;
267-
}
268-
else if (!string.IsNullOrEmpty(metadata?.Authors))
269-
{
270-
_productId = metadata.Authors;
271-
}
272-
273-
// Windows typically doesn't need shell scripts for permissions
274-
_script = string.Empty;
275-
}
276-
277-
/// <summary>
278-
/// Initializes default values specific to Linux platform.
279-
/// </summary>
280-
/// <param name="metadata">The project metadata extracted from csproj file.</param>
281-
private void InitializeLinuxDefaults(ProjectMetadata metadata)
282-
{
283-
// Use the current application's base directory (no admin privileges required)
284-
// This extracts the path from the host program's runtime location
285-
_installPath = AppDomain.CurrentDomain.BaseDirectory;
286-
287-
// Linux uses forward slash as path separator (handled automatically by Path.Combine)
288-
// Linux executables typically don't have .exe extension - prefer detected name from project file
289-
string appNameBase = metadata?.AppName ?? "app";
290-
_appName = appNameBase;
291-
_mainAppName = appNameBase;
292-
293-
// Set version if available from project metadata
294-
if (!string.IsNullOrEmpty(metadata?.Version))
295-
{
296-
_clientVersion = metadata.Version;
297-
_upgradeClientVersion = metadata.Version;
298-
}
299-
300-
// Set product ID from company/authors if available
301-
if (!string.IsNullOrEmpty(metadata?.Company))
302-
{
303-
_productId = metadata.Company;
304-
}
305-
else if (!string.IsNullOrEmpty(metadata?.Authors))
306-
{
307-
_productId = metadata.Authors;
308-
}
309-
310-
// Default shell script for granting file permissions on Linux
311-
_script = @"#!/bin/bash
312-
chmod +x ""$1""
313-
";
314-
}
315-
316-
/// <summary>
317-
/// Initializes default values specific to macOS platform.
318-
/// </summary>
319-
/// <param name="metadata">The project metadata extracted from csproj file.</param>
320-
private void InitializeMacOSDefaults(ProjectMetadata metadata)
321-
{
322-
// Use the current application's base directory (no admin privileges required)
323-
// This extracts the path from the host program's runtime location
324-
_installPath = AppDomain.CurrentDomain.BaseDirectory;
325-
326-
// macOS uses forward slash as path separator (handled automatically by Path.Combine)
327-
// macOS executables typically don't have .exe extension (similar to Linux) - prefer detected name from project file
328-
string appNameBase = metadata?.AppName ?? "app";
329-
_appName = appNameBase;
330-
_mainAppName = appNameBase;
331-
332-
// Set version if available from project metadata
333-
if (!string.IsNullOrEmpty(metadata?.Version))
334-
{
335-
_clientVersion = metadata.Version;
336-
_upgradeClientVersion = metadata.Version;
337-
}
338-
339-
// Set product ID from company/authors if available
340-
if (!string.IsNullOrEmpty(metadata?.Company))
341-
{
342-
_productId = metadata.Company;
343-
}
344-
else if (!string.IsNullOrEmpty(metadata?.Authors))
345-
{
346-
_productId = metadata.Authors;
347-
}
348-
349-
// Default shell script for granting file permissions on macOS
350-
_script = @"#!/bin/bash
351-
chmod +x ""$1""
352-
";
353-
}
354-
35597
/// <summary>
35698
/// Sets the application name (executable to start after update).
35799
/// </summary>

0 commit comments

Comments
 (0)