public Result Run(ConverterOptions opts, Func<string,AssemblyDefinition> resolveAssembly)
 {
     return Run (opts, resolveAssembly, ((path, sourceFile) => {
         var file = Path.GetFileNameWithoutExtension (sourceFile);
         file = Path.Combine (output_path, /*file [0] == 'I' ? file : 'I' +*/ file) + ".cs";
         return file;
     }));
 }
Exemple #2
0
 public Result Run(ConverterOptions opts, Func <string, AssemblyDefinition> resolveAssembly)
 {
     return(Run(opts, resolveAssembly, ((path, sourceFile) => {
         var file = Path.GetFileNameWithoutExtension(sourceFile);
         file = Path.Combine(output_path, /*file [0] == 'I' ? file : 'I' +*/ file) + ".cs";
         return file;
     })));
 }
        public void GenerateCode(CompilationUnit unit, IList<TypeName> parcelableNames, ConverterOptions opts)
        {
            w.WriteLine ("// This file is automatically generated and not supposed to be modified.");
            w.WriteLine ("using System;");
            w.WriteLine ("using Boolean = System.Boolean;");
            w.WriteLine ("using String = System.String;");
            w.WriteLine ("using List = Android.Runtime.JavaList;");
            w.WriteLine ("using Map = Android.Runtime.JavaDictionary;");

            this.unit = unit;
            name_cache = new NameResoltionCache (database, unit, parcelableNames);
            self_ns = name_cache.ToCSharpNamespace (unit.Package);

            if (unit.Imports != null) {
                var nss = new List<string> ();
                foreach (var imp in unit.Imports) {
                    string dummy, pkg = imp.GetPackage ();
                    string ns = database.NamespaceMappings.TryGetValue (pkg, out dummy) ? dummy : imp.GetNamespace ();
                    if (nss.Contains (ns))
                        continue;
                    nss.Add (ns);
                    w.WriteLine ("using " + ns + ";");
                }
            }
            if (unit.Package != null) {
                w.WriteLine ();
                w.WriteLine ("namespace {0}", self_ns);
                w.WriteLine ("{");
            }

            foreach (var type in unit.Types) {
                if (type is Parcelable) {
                    if (type is Parcelable) {
                        switch (opts.ParcelableHandling) {
                        case ParcelableHandling.Ignore:
                            continue;
                        case ParcelableHandling.Error:
                            throw new InvalidOperationException ("Parcelable AIDL cannot be converted to C#");
                        case ParcelableHandling.Stub:
                            StubParcelable ((Parcelable) type);
                            break;
                        }
                    }
                }
                else if (type is Interface)
                    GenerateCode ((Interface) type);
            }

            if (unit.Package != null)
                w.WriteLine ("}");

            this.unit = null;
        }
        public override bool Execute()
        {
            Log.LogDebugMessage ("Task GenerateManagedAidlProxies");
            Log.LogDebugTaskItems ("  References:", References);
            Log.LogDebugTaskItems ("  SourceAidlFiles:", SourceAidlFiles);
            Log.LogDebugMessage ("  IntermediateOutputDirectory: {0}", IntermediateOutputDirectory);
            Log.LogDebugMessage ("  OutputNamespace: {0}", OutputNamespace);
            Log.LogDebugMessage ("  ParcelableHandlingOption: {0}", ParcelableHandlingOption);

            if (SourceAidlFiles.Length == 0) // nothing to do
                return true;

            var opts = new ConverterOptions () {
                Verbose = true,
                ParcelableHandling = ParcelableHandling.Ignore,
            };

            if (!string.IsNullOrEmpty (ParcelableHandlingOption))
                opts.ParcelableHandling = ToParcelableHandling (ParcelableHandlingOption);

            if (!string.IsNullOrEmpty (OutputNamespace))
                opts.OutputNS = OutputNamespace;

            foreach (var file in References)
                opts.AddReference (file.ItemSpec);
            foreach (var file in SourceAidlFiles)
                opts.AddFile (file.ItemSpec);

            var tool = new AidlCompiler ();
            using (var fsw = TextWriter.Null/*File.AppendText (Path.Combine (IntermediateOutputDirectory, "AidlFilesWrittenAbsolute.txt"))*/) {
                tool.FileWritten += (file, source) => Log.LogDebugMessage ("Written ... {0}", file);
                string outPath = Path.Combine (IntermediateOutputDirectory, "aidl");
                var ret = tool.Run (opts, assemblyFile => AssemblyDefinition.ReadAssembly (assemblyFile), (dir, file) => {
                    var dst = Path.GetFullPath (Path.Combine (outPath, Path.ChangeExtension (file, ".cs")));
                    if (!dst.StartsWith (outPath))
                        dst = Path.Combine (outPath, Path.ChangeExtension (Path.GetFileName (file), ".cs"));
                    string dstdir = Path.GetDirectoryName (dst);
                    if (!Directory.Exists (dstdir))
                        Directory.CreateDirectory (dstdir);
                    fsw.WriteLine (dst);
                    fsw.Flush ();
                    return dst;
                });
                if (ret.LogMessages.Count > 0) {
                    foreach (var p in ret.LogMessages)
                        Log.LogError ("{0} {1}: {2}", Path.GetFullPath (p.Key), p.Value.Location, p.Value.ToString ());
                }
            }
            return true;
        }
        // This overload is primarily for unit tests.
        public Result Run(string input, out string output, AssemblyDefinition[] references = null, ParcelableHandling parcelableHandling = ParcelableHandling.Ignore)
        {
            var result   = new Result();
            var database = new BindingDatabase(references ?? Array.Empty <AssemblyDefinition> ());
            var lang     = new LanguageData(new AidlGrammar()
            {
                LanguageFlags = LanguageFlags.Default | LanguageFlags.CreateAst
            });
            var parser = new Parser(lang);

            var pt = parser.Parse(input);

            if (pt.HasErrors())
            {
                foreach (var l in pt.ParserMessages)
                {
                    result.LogMessages.Add("input.aidl", l);
                }

                output = null;
                return(result);
            }

            var unit = (CompilationUnit)pt.Root.AstNode;

            var parcelables = new List <TypeName> ();

            foreach (Parcelable t in unit.Types.Where(t => t is Parcelable))
            {
                parcelables.Add(unit.Package == null ? t.Name : new TypeName(unit.Package.Identifiers.Concat(t.Name.Identifiers).ToArray()));
            }

            var sw   = new StringWriter();
            var opts = new ConverterOptions {
                ParcelableHandling = parcelableHandling
            };

            new CSharpCodeGenerator(sw, database).GenerateCode(unit, parcelables, opts);

            output = sw.ToString();

            return(result);
        }
        public Result Run(ConverterOptions opts, Func<string,AssemblyDefinition> resolveAssembly, Func<string,string,string> outputFilenameSelector)
        {
            var result = new Result ();
            var database = new BindingDatabase (opts.References, resolveAssembly);
            var lang = new LanguageData (new AidlGrammar () { LanguageFlags = LanguageFlags.Default | LanguageFlags.CreateAst });
            var parser = new Parser (lang);
            output_path = opts.OutputDirectory;
            output_ns = opts.OutputNS;
            foreach (var file in opts.InputFiles) {
                var pt = parser.Parse (File.ReadAllText (file), file);
                if (pt.HasErrors ())
                    foreach (var l in pt.ParserMessages)
                        result.LogMessages.Add (file, l);
                else
                    units.Add (file, (CompilationUnit) pt.Root.AstNode);
            }
            if (result.LogMessages.Any (e => e.Value.Level == Irony.ErrorLevel.Error))
                return result;

            if (output_path != null && !Directory.Exists (output_path))
                Directory.CreateDirectory (output_path);
            output_path = output_path ?? "./";

            var parcelables = new List<TypeName> ();
            foreach (var u in units.Values)
                foreach (Parcelable t in u.Types.Where (t => t is Parcelable))
                    parcelables.Add (u.Package == null ? t.Name : new TypeName (u.Package.Identifiers.Concat (t.Name.Identifiers).ToArray ()));

            foreach (var pair in units) {
                string file = outputFilenameSelector (output_path, pair.Key);
                var sw = new StringWriter ();
                new CSharpCodeGenerator (sw, database).GenerateCode (pair.Value, parcelables, opts);
                string csharp = sw.ToString ();
                if (pair.Value.Package != null && output_ns != null)
                    csharp = csharp.Replace (pair.Value.Package.ToString (), output_ns);
                using (var fw = File.CreateText (file))
                    fw.Write (csharp);
                FileWritten (file, csharp);
            }

            return result;
        }
Exemple #7
0
        public Result Run(ConverterOptions opts, Func <string, AssemblyDefinition> resolveAssembly, Func <string, string, string> outputFilenameSelector)
        {
            var result   = new Result();
            var database = new BindingDatabase(opts.References, resolveAssembly);
            var lang     = new LanguageData(new AidlGrammar()
            {
                LanguageFlags = LanguageFlags.Default | LanguageFlags.CreateAst
            });
            var parser = new Parser(lang);

            output_path = opts.OutputDirectory;
            output_ns   = opts.OutputNS;
            foreach (var file in opts.InputFiles)
            {
                var pt = parser.Parse(File.ReadAllText(file), file);
                if (pt.HasErrors())
                {
                    foreach (var l in pt.ParserMessages)
                    {
                        result.LogMessages.Add(file, l);
                    }
                }
                else
                {
                    units.Add(file, (CompilationUnit)pt.Root.AstNode);
                }
            }
            if (result.LogMessages.Any(e => e.Value.Level == Irony.ErrorLevel.Error))
            {
                return(result);
            }

            if (output_path != null && !Directory.Exists(output_path))
            {
                Directory.CreateDirectory(output_path);
            }
            output_path = output_path ?? "./";

            var parcelables = new List <TypeName> ();

            foreach (var u in units.Values)
            {
                foreach (Parcelable t in u.Types.Where(t => t is Parcelable))
                {
                    parcelables.Add(u.Package == null ? t.Name : new TypeName(u.Package.Identifiers.Concat(t.Name.Identifiers).ToArray()));
                }
            }

            foreach (var pair in units)
            {
                string file = outputFilenameSelector(output_path, pair.Key);
                var    sw   = new StringWriter();
                new CSharpCodeGenerator(sw, database).GenerateCode(pair.Value, parcelables, opts);
                string csharp = sw.ToString();
                if (pair.Value.Package != null && output_ns != null)
                {
                    csharp = csharp.Replace(pair.Value.Package.ToString(), output_ns);
                }
                using (var fw = File.CreateText(file))
                    fw.Write(csharp);
                FileWritten(file, csharp);
            }

            return(result);
        }
        public void GenerateCode(CompilationUnit unit, IList <TypeName> parcelableNames, ConverterOptions opts)
        {
            w.WriteLine("// This file is automatically generated and not supposed to be modified.");
            w.WriteLine("using System;");
            w.WriteLine("using Boolean = System.Boolean;");
            w.WriteLine("using String = System.String;");
            w.WriteLine("using List = Android.Runtime.JavaList;");
            w.WriteLine("using Map = Android.Runtime.JavaDictionary;");

            this.unit  = unit;
            name_cache = new NameResoltionCache(database, unit, parcelableNames);
            self_ns    = name_cache.ToCSharpNamespace(unit.Package);

            if (unit.Imports != null)
            {
                var nss = new List <string> ();
                foreach (var imp in unit.Imports)
                {
                    string dummy, pkg = imp.GetPackage();
                    string ns = database.NamespaceMappings.TryGetValue(pkg, out dummy) ? dummy : imp.GetNamespace();
                    if (nss.Contains(ns))
                    {
                        continue;
                    }
                    nss.Add(ns);
                    w.WriteLine("using " + ns + ";");
                }
            }
            if (unit.Package != null)
            {
                w.WriteLine();
                w.WriteLine("namespace {0}", self_ns);
                w.WriteLine("{");
            }

            foreach (var type in unit.Types)
            {
                if (type is Parcelable)
                {
                    if (type is Parcelable)
                    {
                        switch (opts.ParcelableHandling)
                        {
                        case ParcelableHandling.Ignore:
                            continue;

                        case ParcelableHandling.Error:
                            throw new InvalidOperationException("Parcelable AIDL cannot be converted to C#");

                        case ParcelableHandling.Stub:
                            StubParcelable((Parcelable)type);
                            break;
                        }
                    }
                }
                else if (type is Interface)
                {
                    GenerateCode((Interface)type);
                }
            }

            if (unit.Package != null)
            {
                w.WriteLine("}");
            }

            this.unit = null;
        }