├── README.md
├── SwiftClassify.sln
├── SwiftClassify.userprefs
└── SwiftClassify
├── Program.cs
├── Properties
└── AssemblyInfo.cs
└── SwiftClassify.csproj
/README.md:
--------------------------------------------------------------------------------
1 | # SwiftClassify
2 | Helps to insert Objective-C runtime names into a ApiDefinition.cs Xamarin.iOS Binding based on Swift.
3 |
4 |
License
5 | Copyright 2016 Lucas Teixeira
6 |
7 | Licensed under the Apache License, Version 2.0 (the "License");
8 | you may not use this file except in compliance with the License.
9 | You may obtain a copy of the License at
10 |
11 | http://www.apache.org/licenses/LICENSE-2.0
12 |
13 | Unless required by applicable law or agreed to in writing, software
14 | distributed under the License is distributed on an "AS IS" BASIS,
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | See the License for the specific language governing permissions and
17 | limitations under the License.
18 |
--------------------------------------------------------------------------------
/SwiftClassify.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2012
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwiftClassify", "SwiftClassify\SwiftClassify.csproj", "{EAE8F6F8-ABB8-4A28-8B44-6C8094E7EDB0}"
5 | EndProject
6 | Global
7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
8 | Debug|x86 = Debug|x86
9 | Release|x86 = Release|x86
10 | EndGlobalSection
11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
12 | {EAE8F6F8-ABB8-4A28-8B44-6C8094E7EDB0}.Debug|x86.ActiveCfg = Debug|x86
13 | {EAE8F6F8-ABB8-4A28-8B44-6C8094E7EDB0}.Debug|x86.Build.0 = Debug|x86
14 | {EAE8F6F8-ABB8-4A28-8B44-6C8094E7EDB0}.Release|x86.ActiveCfg = Release|x86
15 | {EAE8F6F8-ABB8-4A28-8B44-6C8094E7EDB0}.Release|x86.Build.0 = Release|x86
16 | EndGlobalSection
17 | EndGlobal
18 |
--------------------------------------------------------------------------------
/SwiftClassify.userprefs:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/SwiftClassify/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Text.RegularExpressions;
7 |
8 | namespace SwiftClassify
9 | {
10 | static class MainClass
11 | {
12 | const string SWIFT_PROTOCOL = @"SWIFT_PROTOCOL\(""(?[\w\d]+)""\)\n@protocol\s(?[\w\d]+)";
13 | const string SWIFT_CLASSE = @"SWIFT_CLASS\(""(?[\w\d]+)""\)\n@interface\s(?[\w\d]+)"; // \s:\s([\w\d]+)
14 |
15 | const string API_PROTOCOL = @"\s\[Protocol, Model\]\n\s*(\[(?BaseType\s*\(typeof\([\w\d]+\))\)\]\n\s*)?interface\s*{0}\s";
16 | const string API_CLASSE = @"\s\[(?BaseType\s*\(typeof\([\w\d]+\))\)\]\n\s*(\[[\w\s]+\]\n\s*)*interface\s*{0}\s";
17 |
18 | const string MAPPING = @"(?[\w\d]+)+=(?[\w\d]+)";
19 |
20 | static String StringApi;
21 | static StringBuilder SbApi;
22 | static Dictionary Mappings;
23 |
24 | public static void Main(string[] args)
25 | {
26 | try
27 | {
28 | if (args.Length < 2)
29 | {
30 | Console.WriteLine("Usage: SwiftClassify PATH_TO_SWIFT.H PATH_TO_API_DEFINITION.CS PATH_TO_NAMING_MAP.TXT");
31 | Console.ReadLine();
32 | return;
33 | }
34 |
35 | // Mappings
36 | ParseMapping(args.Length >= 3 ? File.ReadAllText(args[2]) : null);
37 |
38 | // Parse
39 | SbApi = new StringBuilder(StringApi = File.ReadAllText(args[1]));
40 | ModifyApi(GetItens(File.ReadAllText(args[0])));
41 |
42 | // Save
43 | File.WriteAllText(args[1].Replace(".cs", "New.cs"), SbApi.ToString());
44 |
45 | // Ok
46 | Console.WriteLine("Done");
47 | }
48 | catch (Exception ex)
49 | {
50 | Console.WriteLine(ex.Message);
51 | }
52 |
53 | Console.ReadLine();
54 | }
55 |
56 | public static void ParseMapping(string input)
57 | {
58 | if (string.IsNullOrWhiteSpace(input))
59 | {
60 | Mappings = new Dictionary();
61 | }
62 | else
63 | {
64 | var matches = new Regex(MAPPING).Matches(input)
65 | .GetAllMatches()
66 | .Where(c => c.Success);
67 | foreach (var item in matches)
68 | {
69 | Console.WriteLine($"{item.Groups["o"].Value}, {item.Groups["n"].Value}");
70 | }
71 |
72 | Mappings = matches.ToDictionary(k => k.Groups["o"].Value, v => v.Groups["n"].Value);
73 | }
74 | }
75 |
76 | public static Interface[] GetItens(string input)
77 | {
78 | // Protocols
79 | var protocols = from c in new Regex(SWIFT_PROTOCOL).Matches(input).GetAllMatches()
80 | where c.Success
81 | select new Protocol()
82 | {
83 | Name = c.Groups["n"].Value,
84 | CompiledName = c.Groups["i"].Value
85 | } as Interface;
86 |
87 | // Classes
88 | var classes = from c in new Regex(SWIFT_CLASSE).Matches(input).GetAllMatches()
89 | where c.Success
90 | select new Classe()
91 | {
92 | Name = c.Groups["n"].Value,
93 | CompiledName = c.Groups["i"].Value
94 | } as Interface;
95 |
96 | return protocols.Union(classes).ToArray();
97 | }
98 |
99 | public static void ModifyApi(Interface[] itens)
100 | {
101 | foreach (var item in itens)
102 | {
103 | item.Replace();
104 | }
105 | }
106 |
107 | public static Match[] GetAllMatches(this MatchCollection matches)
108 | {
109 | Match[] matchArray = new Match[matches.Count];
110 | matches.CopyTo(matchArray, 0);
111 |
112 | return matchArray;
113 | }
114 |
115 |
116 | public abstract class Interface
117 | {
118 | public string Name { get; set; }
119 | public string CompiledName { get; set; }
120 |
121 | protected string FinalName
122 | {
123 | get
124 | {
125 | if (Mappings.Keys.Contains(Name)) return Mappings[Name];
126 |
127 | return Name;
128 | }
129 | }
130 |
131 | public abstract void Replace();
132 | }
133 |
134 | public class Protocol : Interface
135 | {
136 | public override void Replace()
137 | {
138 | var regex = new Regex(string.Format(API_PROTOCOL, FinalName)).Match(StringApi);
139 |
140 | string oldValue = regex.Value;
141 | string newValue = string.Empty;
142 |
143 | if (regex.Groups["b"].Success)
144 | {
145 | var baseType = regex.Groups["b"].Value;
146 | newValue = oldValue.Replace(baseType, $@"{baseType}, Name = ""{CompiledName}""");
147 | }
148 | else
149 | {
150 | newValue = oldValue.Replace("Protocol", $@"Protocol(Name = ""{CompiledName}"")");
151 | }
152 |
153 | SbApi.Replace(oldValue, newValue);
154 | }
155 | }
156 |
157 | public class Classe : Interface
158 | {
159 | public override void Replace()
160 | {
161 | var regex = new Regex(string.Format(API_CLASSE, FinalName)).Match(StringApi);
162 | var baseType = regex.Groups["b"].Value;
163 |
164 | string oldValue = regex.Value;
165 | string newValue = oldValue.Replace(baseType, $@"{baseType}, Name = ""{CompiledName}""");
166 |
167 | SbApi.Replace(oldValue, newValue);
168 | }
169 | }
170 | }
171 | }
172 |
--------------------------------------------------------------------------------
/SwiftClassify/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 |
4 | // Information about this assembly is defined by the following attributes.
5 | // Change them to the values specific to your project.
6 |
7 | [assembly: AssemblyTitle("SwiftClassify")]
8 | [assembly: AssemblyDescription("")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("")]
12 | [assembly: AssemblyCopyright("(c) Lucas Teixeira")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision,
18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision.
19 |
20 | [assembly: AssemblyVersion("1.0.*")]
21 |
22 | // The following attributes are used to specify the signing key for the assembly,
23 | // if desired. See the Mono documentation for more information about signing.
24 |
25 | //[assembly: AssemblyDelaySign(false)]
26 | //[assembly: AssemblyKeyFile("")]
27 |
28 |
--------------------------------------------------------------------------------
/SwiftClassify/SwiftClassify.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | x86
6 | {EAE8F6F8-ABB8-4A28-8B44-6C8094E7EDB0}
7 | Exe
8 | SwiftClassify
9 | SwiftClassify
10 | v4.5
11 |
12 |
13 | true
14 | full
15 | false
16 | bin\Debug
17 | DEBUG;
18 | prompt
19 | 4
20 | true
21 | x86
22 |
23 |
24 | true
25 | bin\Release
26 | prompt
27 | 4
28 | true
29 | x86
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------