static int Main(string[] args)
        {
            bool          verbose    = false;
            string        target     = null;
            string        complist   = null;
            string        targetdir  = ".";
            List <string> references = new List <string>();

            bool      nologo = false;
            bool      help   = false;
            OptionSet p      = new OptionSet()
            {
                { "v|verbose", "Verbose output", v => verbose = v != null },
                { "t|target=", "Target assembly name", v => target = v },
                { "c|complist=", "licx file to compile", v => complist = v },
                { "i|load=", "Reference to load", v => { if (v != null)
                                                         {
                                                             references.Add(v);
                                                         }
                  } },
                { "o|outdir=", "Output directory for the .licenses file", v => targetdir = v },
                { "nologo", "Do not display logo", v => nologo = null != v },
                { "h|?|help", "Show help", v => help = v != null }
            };
            List <string> extra;

            try
            {
                extra = p.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine("lc: " + e.Message);
                Console.WriteLine("try lc --help for more information");
                return(1);
            }
            if (!nologo)
            {
                Console.WriteLine("Mono License Compiler");
                Console.WriteLine("Copyright (c) 2009 by RemObjects Software");
            }
            if (help)
            {
                Console.WriteLine();
                Console.WriteLine("lc -c filename -t targetassembly [-i references] [-v] [-o] [-nologo]");
                Console.WriteLine();
                Console.WriteLine("Options:");
                p.WriteOptionDescriptions(Console.Out);
                return(1);
            }
            if (extra.Count > 0)
            {
                Console.WriteLine("Unexpected arguments passed on cmd line");
                return(1);
            }
            if (target == null || complist == null)
            {
                Console.WriteLine("No target/complist passed");
                return(1);
            }
            try
            {
                if (!File.Exists(complist))
                {
                    Console.WriteLine("Could not find file: " + complist);
                    return(1);
                }

                LCLicenseContext ctx = new LCLicenseContext();
                ctx.LicxFilename = complist;
                if (verbose)
                {
                    Console.WriteLine("Input file: " + complist);
                }
                ctx.OutputFilename = Path.Combine(targetdir ?? ".", target) + ".licenses";
                if (verbose)
                {
                    Console.WriteLine("Output filename: " + ctx.OutputFilename);
                }
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
                privatePaths.Add(".");
                Dictionary <string, Assembly> loaded = new Dictionary <string, Assembly>();
                foreach (string reference in references)
                {
                    string path = Path.GetDirectoryName(reference);
                    if (!privatePaths.Contains(path))
                    {
                        if (verbose)
                        {
                            Console.WriteLine("Adding " + Path.GetDirectoryName(reference) + " to private paths");
                        }
                        privatePaths.Add(path);
                    }
                    Assembly asm = Assembly.LoadFrom(reference);
                    loaded.Add(asm.GetName().Name, asm);
                    if (verbose)
                    {
                        Console.WriteLine("Loaded assembly: " + asm.GetName().ToString());
                    }
                }

                using (StreamReader sr = new StreamReader(complist))
                {
                    int    lineno = 0;
                    string line   = "";
                    while (sr.Peek() != -1)
                    {
                        try
                        {
                            line = sr.ReadLine();
                            if (line == null || line == "" || line[0] == '#')
                            {
                                continue;
                            }
                            if (verbose)
                            {
                                Console.WriteLine("Generating license for: " + line);
                            }

                            string[] sLine = line.Split(new char[] { ',' }, 2);
                            Type     stype = null;
                            if (sLine.Length == 1)
                            {
                                stype = Type.GetType(line, false, true);
                                if (stype == null)
                                {
                                    foreach (KeyValuePair <string, Assembly> et in loaded)
                                    {
                                        stype = et.Value.GetType(sLine[0], false, true);
                                        if (stype != null)
                                        {
                                            if (verbose)
                                            {
                                                Console.WriteLine("Found type in " + et.Key);
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (sLine[1].IndexOf(',') >= 0)
                                {
                                    stype = Type.GetType(line, false, true);
                                }
                                else
                                {
                                    string s = sLine[1].Trim();
                                    foreach (KeyValuePair <string, Assembly> et in loaded)
                                    {
                                        if (String.Compare(et.Key, s, true, CultureInfo.InvariantCulture) == 0)
                                        {
                                            stype = et.Value.GetType(sLine[0], false, true);
                                            if (stype != null)
                                            {
                                                if (verbose)
                                                {
                                                    Console.WriteLine("Found type in " + et.Key);
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    if (stype == null)
                                    {
                                        foreach (KeyValuePair <string, Assembly> et in loaded)
                                        {
                                            stype = et.Value.GetType(sLine[0], false, true);
                                            if (stype != null)
                                            {
                                                if (verbose)
                                                {
                                                    Console.WriteLine("Found type in " + et.Key);
                                                }
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (stype == null)
                            {
                                throw new Exception("Unable to find type: " + line);
                            }
                            LicenseManager.CreateWithContext(stype, ctx);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Exception during compiling " + complist + ": " + lineno);
                            Console.WriteLine(e.ToString());
                        }
                    }
                }

                using (FileStream fs = new FileStream(ctx.OutputFilename, FileMode.Create))
                {
                    try
                    {
                        DesigntimeLicenseContextSerializer.Serialize(fs, target.ToUpper(CultureInfo.InvariantCulture), ctx);
                    }
                    catch {}
                    if (fs.Length == 0) // older mono does not support this, but when it does, we should use the proper version.
                    {
                        IntSerialize(fs, target.ToUpper(CultureInfo.InvariantCulture), ctx);
                    }
                }
                if (verbose)
                {
                    Console.WriteLine("Saved to: " + Path.GetFullPath(ctx.OutputFilename));
                }
                return(0);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.ToString());
                return(1);
            }
        }
示例#2
0
文件: lc.cs 项目: nobled/mono
        static int Main(string[] args)
        {
            bool verbose = false;
            string target = null;
            string complist = null;
            string targetdir = ".";
            List<string> references = new List<string>();

            bool nologo = false;
            bool help = false;
            OptionSet p = new OptionSet() {
                {"v|verbose", "Verbose output", v => verbose = v!= null },
                {"t|target=", "Target assembly name", v => target = v },
                {"c|complist=","licx file to compile", v => complist = v },
                {"i|load=", "Reference to load", v=> {if (v != null) references.Add(v);}},
                {"o|outdir=", "Output directory for the .licenses file", v=> targetdir = v },
                {"nologo", "Do not display logo", v=> nologo = null != v },
                {"h|?|help", "Show help", v=>help = v != null }
            };
            List<string> extra;
            try
            {
                extra = p.Parse(args);
            }
            catch(OptionException e) 
            {
                Console.WriteLine("lc: " + e.Message);
                Console.WriteLine("try lc --help for more information");
                return 1;
            }
            if (!nologo) {
                Console.WriteLine("Mono License Compiler");
                Console.WriteLine("Copyright (c) 2009 by RemObjects Software");
            }
            if (help) {
                Console.WriteLine();
                Console.WriteLine("lc -c filename -t targetassembly [-i references] [-v] [-o] [-nologo]");
                Console.WriteLine();
                Console.WriteLine("Options:");
                p.WriteOptionDescriptions(Console.Out);
                return 1;
            }
            if (extra.Count > 0) {
                Console.WriteLine("Unexpected arguments passed on cmd line");
                return 1;
            }
            if (target == null || complist == null){
                Console.WriteLine("No target/complist passed");
                return 1;
            }
            try {
                if (!File.Exists(complist)) {
                    Console.WriteLine("Could not find file: "+complist);
                    return 1;
                }

                LCLicenseContext ctx = new LCLicenseContext();
                ctx.LicxFilename = complist;
                if (verbose) Console.WriteLine("Input file: "+complist);
                ctx.OutputFilename = Path.Combine(targetdir ??".", target)+".licenses";
                if (verbose) Console.WriteLine("Output filename: "+ctx.OutputFilename);
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
                privatePaths.Add(".");
                Dictionary<string, Assembly> loaded = new Dictionary<string, Assembly>();
                foreach (string reference in references) {
                    string path = Path.GetDirectoryName(reference);
                    if (!privatePaths.Contains(path))
                    {
                        if (verbose) Console.WriteLine("Adding " + Path.GetDirectoryName(reference) + " to private paths");
                        privatePaths.Add(path);
                       }
                    Assembly asm = Assembly.LoadFrom(reference);
                    loaded.Add(asm.GetName().Name, asm);
                    if (verbose) Console.WriteLine("Loaded assembly: "+asm.GetName().ToString());

                }

                using (StreamReader sr = new StreamReader(complist))
                {
                    int lineno = 0;
                    string line = "";
                    while (sr.Peek() != -1)
                    {
                        try
                        {
                            line = sr.ReadLine();
                            if (line == null || line == "" || line[0] == '#' ) continue;
                            if (verbose) Console.WriteLine("Generating license for: "+line);

                            string[] sLine = line.Split(new char[] { ',' }, 2);
                            Type stype = null;
                            if (sLine.Length == 1)
                            {
                                stype = Type.GetType(line, false, true);
                                if (stype == null)
                                {
                                    foreach (KeyValuePair<string, Assembly> et in loaded)
                                    {
                                        stype = et.Value.GetType(sLine[0], false, true);
                                        if (stype != null) {
                                            if (verbose) Console.WriteLine("Found type in "+et.Key);
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (sLine[1].IndexOf(',') >= 0)
                                {
                                    stype = Type.GetType(line, false, true);
                                }
                                else
                                {
                                    string s = sLine[1].Trim();
                                    foreach (KeyValuePair<string, Assembly> et in loaded)
                                    {
                                        if (String.Compare(et.Key, s, true, CultureInfo.InvariantCulture) == 0)
                                        {
                                            stype = et.Value.GetType(sLine[0], false, true);
                                            if (stype != null) {
                                                if (verbose) Console.WriteLine("Found type in "+et.Key);
                                                break;
                                            }
                                        }
                                    }
                                    if (stype == null)
                                    {
                                        foreach (KeyValuePair<string, Assembly> et in loaded)
                                        {
                                            stype = et.Value.GetType(sLine[0], false, true);
                                            if (stype != null) {
                                                if (verbose) Console.WriteLine("Found type in "+et.Key);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (stype == null)
                                throw new Exception("Unable to find type: " + line);
                            LicenseManager.CreateWithContext(stype, ctx);
                        }
                        catch(Exception e)
                        {
                            Console.WriteLine("Exception during compiling " + complist + ": " + lineno);
                            Console.WriteLine(e.ToString());
                        }
                    }
                }

                using (FileStream fs = new FileStream(ctx.OutputFilename, FileMode.Create)) {
                    try {
                    DesigntimeLicenseContextSerializer.Serialize(fs, target.ToUpper(CultureInfo.InvariantCulture), ctx);
                    } catch {}
                    if (fs.Length == 0) // older mono does not support this, but when it does, we should use the proper version.
                        IntSerialize(fs, target.ToUpper(CultureInfo.InvariantCulture), ctx);
                }
                if (verbose)
                    Console.WriteLine("Saved to: "+ Path.GetFullPath(ctx.OutputFilename));
                return 0;
            } catch(Exception e){
                Console.WriteLine("Exception: "+e.ToString());
                return 1;
            }

        }