Ejemplo n.º 1
0
        /// <summary>
        /// Writes the section using an XML writer
        /// </summary>
        /// <param name="writer">XML writer to use</param>
        /// <param name="project">The project to generate .csproj for</param>
        /// <param name="context">Current .csproj generation context</param>
        public override void Write(XmlWriter writer, Project project, IMSBuildProjectGeneratorContext context)
        {
            writer.WriteStartElement("ItemGroup");

            var orderedFiles = GetOrderedFiles(project);

            foreach (var pair in orderedFiles)
            {
                var file          = pair.Item1;
                var sourceSetType = pair.Item2;

                var relativePath = ToProjectRelativePath(project, file, ProjectSourceSetName);
                var logicalPath  = GetLogicalPath(project, file, sourceSetType);

                // We have to skip .csproj files, which are generated by bari to the source set because otherwise
                // VisualStudio does not work as expected:
                if (!IgnoredExtensions.Any(ext => relativePath.EndsWith(ext, StringComparison.InvariantCultureIgnoreCase)))
                {
                    writer.WriteStartElement(GetElementNameFor(project, file));
                    writer.WriteAttributeString("Include", relativePath);

                    if (ProjectSourceSetName != sourceSetType)
                    {
                        writer.WriteElementString("LogicalName", logicalPath);
                    }

                    WriteAdditionalOptions(writer, project, file);

                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
        }
Ejemplo n.º 2
0
        public static void WriteFilesToDatabase(DbFileContext ctx, Uri initialUri, DirectoryInfo root, int?id)
        {
            string virtualPath;
            string dirName;

            if (id == null)
            {
                virtualPath = "/";
                dirName     = null;
            }
            else
            {
                var currentUri   = new Uri(root.FullName);
                var tempRelative = initialUri.MakeRelativeUri(currentUri).ToString();
                var iof          = tempRelative.IndexOf('/');
                virtualPath = tempRelative.Substring(iof);

                dirName = root.Name;
            }

            foreach (var ignoredDirectory in IgnoredDirectories)
            {
                if (virtualPath.StartsWith(ignoredDirectory, StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
            }

            var dbFile = new DbFile
            {
                IsDirectory = true,
                Name        = dirName,
                VirtualPath = virtualPath,
                ParentId    = id
            };

            ctx.DbFiles.Add(dbFile);
            ctx.SaveChanges();

            foreach (var fi in root.EnumerateFiles())
            {
                bool ignore = IgnoredExtensions.Any(ignoredExtension => fi.Extension.StartsWith(ignoredExtension)) ||
                              IgnoredFiles.Any(x => x.Equals(fi.Name, StringComparison.OrdinalIgnoreCase));

                if (ignore)
                {
                    continue;
                }

                Console.WriteLine(fi.FullName);

                var dbFileFolder = new DbFile
                {
                    IsDirectory = false,
                    Name        = Path.GetFileNameWithoutExtension(fi.Name),
                    Extension   = fi.Extension,
                    VirtualPath = Path.Combine(virtualPath, fi.Name).Replace('\\', '/'),
                    ParentId    = dbFile.Id,
                };

                if (IsTextFile(fi.Extension))
                {
                    var text = File.ReadAllText(fi.FullName, Encoding.UTF8);
                    dbFileFolder.Texto = text;
                }
                else
                {
                    var bytes = File.ReadAllBytes(fi.FullName);
                    dbFileFolder.Bytes    = bytes;
                    dbFileFolder.IsBinary = true;
                }

                ctx.DbFiles.Add(dbFileFolder);
                ctx.SaveChanges();
            }

            foreach (var di in root.EnumerateDirectories())
            {
                WriteFilesToDatabase(ctx, initialUri, di, dbFile.Id);
            }
        }