Ejemplo n.º 1
0
 public static void Main(string[] args)
 {
     new Compiler(@"c:\Program Files (x86)\Microsoft Visual Studio\VB98\VB6.EXE").Compile(
         VB6Project.Load(@"C:\dev\Cogito.VisualBasic6\sample\Cogito.VisualBasic6.Sample\VB6Sample.vbp"),
         @"C:\dev\Cogito.VisualBasic6\sample\Cogito.VisualBasic6.Sample\obj\Debug\net47",
         out var errors);
 }
Ejemplo n.º 2
0
        public VB6Project ParseProjectFile(string projectFilePath)
        {
            StreamReader sr = null;

            try
            {
                _vb6Project = new VB6Project();
                _vb6Project.ProjectFileName = Path.GetFileName(projectFilePath);

                // Note: This would assumed all source code have unique folder name, which is the case in Globe. Just using this as logical key to delete data.
                // Didn't handle scenario where same folder name exist in subfolder or even vbp file located at C:\Project1.vbp (These are not typical Globe scenario...)
                _vb6Project.ProjectFolderName = new DirectoryInfo(projectFilePath).Parent.Name;

                string line;
                sr = new StreamReader(projectFilePath);
                while ((line = sr.ReadLine()) != null)
                {
                    Parse(line);
                }

                return(_vb6Project);
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }
            }
        }
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            Log.LogMessage("Source: {0}", Source);
            Log.LogMessage("Target: {0}", Target);

            TransformVbp(VB6Project.Load(Source)).Save(Target);
            return(true);
        }
        /// <summary>
        /// Applies the transforms.
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        VB6Project TransformVbp(VB6Project project)
        {
            if (References != null &&
                References.Length > 0)
            {
                project.References.Clear();
                project.References.AddRange(GetReferencesForTaskItems(References).Where(i => i != null));
            }

            if (Modules != null &&
                Modules.Length > 0)
            {
                project.Modules.Clear();
                project.Modules.AddRange(GetModulesForTaskItems(Modules));
            }

            if (Classes != null &&
                Classes.Length > 0)
            {
                project.Classes.Clear();
                project.Classes.AddRange(GetClassesForTaskItems(Classes));
            }

            if (Forms != null &&
                Forms.Length > 0)
            {
                project.Forms.Clear();
                project.Forms.AddRange(GetFormsForTaskItems(Forms));
            }

            if (!string.IsNullOrWhiteSpace(Properties))
            {
                foreach (var kvp in Properties
                         .Split(';')
                         .Select(i => i.Split('='))
                         .Where(i => i.Length == 2)
                         .Where(i => !string.IsNullOrWhiteSpace(i[0]))
                         .Where(i => !string.IsNullOrWhiteSpace(i[1])))
                {
                    var key = kvp[0].Trim();
                    var val = kvp[1].Trim();
                    var typ = GetPropertyValueType(project, key, val);
                    if (typ != null)
                    {
                        project.Properties[key] = Convert.ChangeType(val, typ);
                    }
                }
            }

            return(project);
        }
        /// <summary>
        /// Attempt to get the appropriate property value type for the given key.
        /// </summary>
        /// <param name="project"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Type GetPropertyValueType(VB6Project project, string key, string value)
        {
            // project might already have a known value
            if (project.Properties.ContainsKey(key))
            {
                var exist = project.Properties[key];
                if (exist != null)
                {
                    return(exist.GetType());
                }
            }

            // convert to int
            if (int.TryParse(value, out var intValue))
            {
                return(typeof(int));
            }

            // default to string
            return(typeof(string));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Begins compilation. Returns <c>true</c> for successful compilation.
        /// </summary>
        /// <param name="vb6"></param>
        /// <param name="project"></param>
        /// <param name="output"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public bool Compile(VB6Project project, string output, out IList <string> errors)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            // create missing directory
            if (Directory.Exists(output) == false)
            {
                Directory.CreateDirectory(output);
            }

            var source = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetRandomFileName(), ".vbp"));

            try
            {
                project.Save(source);

                var sti = new ProcessStartInfo();
                sti.FileName               = typeof(Executor).Assembly.Location;
                sti.Arguments              = $@"-e ""{VB6Exe}"" -v ""{source}"" -o ""{output}""";
                sti.UseShellExecute        = false;
                sti.CreateNoWindow         = true;
                sti.RedirectStandardOutput = true;
                sti.RedirectStandardError  = true;

                using (var prc = Process.Start(sti))
                {
                    var stderr = new List <string>();
                    prc.ErrorDataReceived += (s, a) => stderr.Add(a.Data);
                    prc.BeginErrorReadLine();
                    prc.BeginOutputReadLine();
                    prc.WaitForExit();

                    if (prc.ExitCode != 0)
                    {
                        errors = stderr.Select(i => i?.Trim()).Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
                        return(false);
                    }
                }
            }
            finally
            {
                try
                {
                    if (PreserveTemporary == false)
                    {
                        if (File.Exists(source))
                        {
                            File.Delete(source);
                        }
                    }
                }
                catch
                {
                }
            }

            errors = null;
            return(true);
        }