-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
118 lines (96 loc) · 4.04 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
namespace Manifest
{
public enum ExitCode
{
Success = 0,
EmptyOrNullVersionCode = 1,
InvalidFilename = 2,
NotEnoughParameters = 3
}
public static class ManifestConsole
{
private static string Header()
{
var builder = new StringBuilder();
builder.AppendLine("=====================================");
builder.AppendLine($"Android Manifest Editor v{Assembly.GetCallingAssembly().GetName().Version})");
builder.AppendLine("=====================================");
return builder.ToString();
}
private static string Help()
{
var builder = new StringBuilder();
builder.AppendLine("=====================================");
builder.AppendLine($"Android Manifest Editor v{Assembly.GetCallingAssembly().GetName().Version})");
builder.AppendLine("=====================================");
builder.AppendLine("Usage: setversioncode filename versionCode packageid");
builder.AppendLine("Example:setversioncode manifest.xml 150 com.acme.dev");
return builder.ToString();
}
private static ExitCode ValidateParameters(string[] args)
{
// At least two parameters
return args.Length < 2 ? ExitCode.NotEnoughParameters : ExitCode.Success;
}
public static int Main(string[] args)
{
Console.WriteLine(Header());
//validations
var isValid = ValidateParameters(args);
if (isValid != ExitCode.Success)
{
Console.Write(Help());
return (int) isValid;
}
var versionCode = args[1];
var manifestFilePath = args[0];
var packageId = string.Empty;
if (args.Length >=3) packageId = args[2];
try
{
//var file = System.IO.File.ReadAllText(manifestFilePath);
var element = XElement.Load(manifestFilePath);
var attributes = element.Attributes().ToList();
// Update Version Code
var versionAttribute = attributes
.FirstOrDefault(a => a.Name.LocalName.Equals("versionCode"));
if (versionAttribute != null)
versionAttribute.Value = versionCode;
// Update Package ID
var packageAttribute = attributes
.FirstOrDefault(a => a.Name.LocalName.Equals("package"));
if (packageAttribute != null && !string.IsNullOrEmpty(packageId))
packageAttribute.Value = packageId;
//android:
//authorities
var authoritiesAttribute = GetAuthoritiesAttribute(element);
if (authoritiesAttribute != null)
authoritiesAttribute.Value = packageId;
element.Save(manifestFilePath );
}
catch (FileNotFoundException)
{
Console.WriteLine("Manifest File not found", manifestFilePath);
return (int) ExitCode.InvalidFilename;
}
Console.WriteLine("Your manifest file has been changed successfully");
// Console.WriteLine("Press ENTER to continue....");
Console.ReadLine();
return (int) ExitCode.Success;
}
private static XAttribute GetAuthoritiesAttribute(XElement element)
{
var applicationElement = element.Elements().FirstOrDefault(x => x.Name == "application");
var providerElement = applicationElement?.Elements().Where(x => x.Name == "provider");
var authoritiesAttribute =
providerElement?.Attributes().FirstOrDefault(x => x.Name.LocalName.Equals("authorities"));
return authoritiesAttribute;
}
}
}