예제 #1
0
        public static Scaffold Parse(string path)
        {
            XDocument document = XDocument.Parse(FileUtilities.DataStore.ReadFileAsText(path));
            Scaffold  scaffold = new Scaffold();

            foreach (XElement fileElement in document.Root.Elements(XName.Get("ScaffoldFile")))
            {
                ScaffoldFile scaffoldFile = new ScaffoldFile();
                scaffoldFile.Path            = GetAttribute(fileElement, "Path");
                scaffoldFile.Copy            = bool.Parse(GetAttribute(fileElement, "Copy") ?? "true");
                scaffoldFile.TargetPath      = GetAttribute(fileElement, "TargetPath");
                scaffoldFile.PathExpression  = GetAttribute(fileElement, "PathExpression");
                scaffoldFile.SourceDirectory = GetAttribute(fileElement, "SourceDirectory");

                foreach (XElement ruleElement in fileElement.Elements())
                {
                    string[]     names      = ruleElement.Name.LocalName.Split('.');
                    string       className  = string.Format("{0}.{1}", typeof(Scaffold).Namespace, names[0]);
                    string       methodName = names[1];
                    Type         type       = typeof(Scaffold).Assembly.GetType(className);
                    MethodInfo   ruleInfo   = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public);
                    ScaffoldRule rule       = Delegate.CreateDelegate(typeof(ScaffoldRule), ruleInfo) as ScaffoldRule;
                    scaffoldFile.Rules.Add(rule);
                }

                scaffold.Files.Add(scaffoldFile);
            }

            return(scaffold);
        }
예제 #2
0
        public static void GenerateScaffolding(string sourcePath, string destPath, Dictionary <string, object> parameters)
        {
            sourcePath = Path.GetFullPath(sourcePath);
            Scaffold scaffold = Parse(Path.Combine(sourcePath, Resources.ScaffoldXml));

            foreach (ScaffoldFile file in scaffold.Files)
            {
                string sourceDirectory;
                if (string.IsNullOrEmpty(file.SourceDirectory) ||
                    !parameters.ContainsKey(file.SourceDirectory))
                {
                    sourceDirectory = sourcePath;
                }
                else
                {
                    sourceDirectory = (string)parameters[file.SourceDirectory];
                }


                foreach (string path in GetPaths(sourceDirectory, file))
                {
                    string tempPath;

                    if (string.IsNullOrEmpty(file.PathExpression))
                    {
                        tempPath = !string.IsNullOrEmpty(file.TargetPath) ? file.TargetPath : GetRelativePath(sourceDirectory, path);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(file.TargetPath))
                        {
                            tempPath = GetRelativePath(sourceDirectory, path);
                        }
                        else
                        {
                            tempPath = GetRelativePath(sourceDirectory, path);
                            string t1 = tempPath.Substring(tempPath.IndexOf('\\') + 1);
                            tempPath = Path.Combine(file.TargetPath, t1);
                        }
                    }

                    tempPath = Path.Combine(destPath, tempPath);
                    FileUtilities.DataStore.CreateDirectory(Path.GetDirectoryName(tempPath));

                    FileUtilities.DataStore.CopyFile(path, tempPath);

                    foreach (ScaffoldRule rule in file.Rules)
                    {
                        rule(tempPath, parameters);
                    }

                    if (!file.Copy)
                    {
                        FileUtilities.DataStore.DeleteFile(tempPath);
                    }
                }
            }
        }
예제 #3
0
        public static Scaffold Parse(string path)
        {
            XDocument document = XDocument.Load(path);
            Scaffold scaffold = new Scaffold();

            foreach (XElement fileElement in document.Root.Elements(XName.Get("ScaffoldFile")))
            {
                ScaffoldFile scaffoldFile = new ScaffoldFile();
                scaffoldFile.Path = GetAttribute(fileElement, "Path");
                scaffoldFile.Copy = bool.Parse(GetAttribute(fileElement, "Copy") ?? "true");
                scaffoldFile.TargetPath = GetAttribute(fileElement, "TargetPath");
                scaffoldFile.PathExpression = GetAttribute(fileElement, "PathExpression");
                scaffoldFile.SourceDirectory = GetAttribute(fileElement, "SourceDirectory");

                foreach (XElement ruleElement in fileElement.Elements())
                {
                    string[] names = ruleElement.Name.LocalName.Split('.');
                    string className = string.Format("{0}.{1}", typeof(Scaffold).Namespace, names[0]);
                    string methodName = names[1];
                    Type type = typeof(Scaffold).Assembly.GetType(className);
                    MethodInfo ruleInfo = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public);
                    ScaffoldRule rule = Delegate.CreateDelegate(typeof(ScaffoldRule), ruleInfo) as ScaffoldRule;
                    scaffoldFile.Rules.Add(rule);
                }

                scaffold.Files.Add(scaffoldFile);
            }

            return scaffold;
        }