示例#1
0
        private static GlobalContext OpenContext(string databaseFullPath)
        {
            ProcessesHelpers.KillOwnProcessesByName("1cv8.exe");
            var connectionStringBuilder = new ConnectionStringBuilder
            {
                Type         = Connection1CType.File,
                FileLocation = databaseFullPath,
                User         = "******",
                Password     = ""
            };
            var globalContextFactory   = new GlobalContextFactory();
            var globalContextComObject = globalContextFactory.Create(connectionStringBuilder.GetConnectionString());

            return(new GlobalContext(globalContextComObject));
        }
示例#2
0
        private static int GenCsMeta(NameValueCollection parameters)
        {
            var connectionString       = parameters["connection-string"];
            var resultAssemblyFullPath = parameters["result-assembly-full-path"];
            var namespaceRoot          = parameters["namespace-root"];
            var scanItems          = (parameters["scan-items"] ?? "").Split(',');
            var sourcePath         = parameters["source-path"];
            var csprojFilePath     = parameters["csproj-file-path"];
            var parametersAreValid =
                !string.IsNullOrEmpty(connectionString) &&
                (!string.IsNullOrEmpty(resultAssemblyFullPath) || !string.IsNullOrEmpty(sourcePath)) &&
                !string.IsNullOrEmpty(namespaceRoot) &&
                scanItems.Length > 0;

            if (!parametersAreValid)
            {
                Console.Out.WriteLine("Invalid arguments");
                Console.Out.WriteLine(
                    "Usage: Generator.exe -cmd gen-cs-meta -connection-string <string> [-result-assembly-full-path <path>] -namespace-root <namespace> -scanItems Справочник.Банки,Документ.СписаниеСРасчетногоСчета [-source-path <sourcePath>] [-csproj-file-path]");
                return(-1);
            }
            object globalContext = null;

            LogHelpers.LogWithTiming(string.Format("connecting to [{0}]", connectionString),
                                     () => globalContext = new GlobalContextFactory().Create(connectionString));

            sourcePath = sourcePath ?? GetTemporaryDirectoryFullPath();
            if (Directory.Exists(sourcePath))
            {
                Directory.Delete(sourcePath, true);
            }
            string[] fileNames = null;
            LogHelpers.LogWithTiming(string.Format("generating code into [{0}]", sourcePath),
                                     () =>
            {
                var generator = new ObjectModelGenerator(globalContext,
                                                         scanItems, namespaceRoot, sourcePath);
                fileNames = generator.Generate().ToArray();
            });

            if (!string.IsNullOrEmpty(csprojFilePath))
            {
                csprojFilePath = Path.GetFullPath(csprojFilePath);
                if (!File.Exists(csprojFilePath))
                {
                    Console.Out.WriteLine("proj file [{0}] does not exist, create it manually for the first time",
                                          csprojFilePath);
                    return(-1);
                }
                LogHelpers.LogWithTiming(string.Format("patching proj file [{0}]", csprojFilePath),
                                         () =>
                {
                    var updater = new CsProjectFileUpdater(csprojFilePath, sourcePath);
                    updater.Update();
                });
            }

            if (!string.IsNullOrEmpty(resultAssemblyFullPath))
            {
                LogHelpers.LogWithTiming(string.Format("compiling [{0}] to assembly [{1}]",
                                                       sourcePath, resultAssemblyFullPath), () =>
                {
                    var cSharpCodeProvider = new CSharpCodeProvider();
                    var compilerParameters = new CompilerParameters
                    {
                        OutputAssembly          = resultAssemblyFullPath,
                        GenerateExecutable      = false,
                        GenerateInMemory        = false,
                        IncludeDebugInformation = false
                    };
                    var linqTo1CFilePath = PathHelpers.AppendBasePath("Simple1C.dll");
                    compilerParameters.ReferencedAssemblies.Add(linqTo1CFilePath);
                    var compilerResult = cSharpCodeProvider.CompileAssemblyFromFile(compilerParameters, fileNames);
                    if (compilerResult.Errors.Count > 0)
                    {
                        Console.Out.WriteLine("compile errors");
                        foreach (CompilerError error in compilerResult.Errors)
                        {
                            Console.Out.WriteLine(error);
                            Console.Out.WriteLine("===================");
                        }
                    }
                });
            }
            return(0);
        }