Beispiel #1
0
 public Program(
     bool isMigration,
     string srcDirectory,
     string destDirectory,
     AzureFileInformationCollection azureFileInformationCollection)
 {
     _isMigration   = isMigration;
     _srcDirectory  = srcDirectory;
     _destDirectory = destDirectory;
     _azureFileInformationCollection = azureFileInformationCollection;
 }
Beispiel #2
0
        private static int Main(string[] args)
        {
            try
            {
                var exitCode = 0;
                if (args.Length != 3 && args.Length != 4)
                {
                    PrintUsage();
                    return(1);
                }

                var rewriterToolArguments = ParseRewriterToolArgumentsFile(args[0], args[1], args[2]);
                if (rewriterToolArguments == null)
                {
                    return(1);
                }

                AzureFileInformationCollection azureFileInformationCollection = new AzureFileInformationCollection();
                if (args.Length == 4)
                {
                    azureFileInformationCollection.AzureVideoInfoMapping = AzureVideoHelper.ParseAzureVideoFile(args[3], rewriterToolArguments.IsMigration);
                }

                if (rewriterToolArguments.IsMigration)
                {
                    GenerateAzureFileInfoForMigration(args[0], rewriterToolArguments, args[2], azureFileInformationCollection);
                }
                else
                {
                    GenerateAzureFileInfo(args[0], rewriterToolArguments, args[2], azureFileInformationCollection);
                }

                foreach (var azureTransformArguments in rewriterToolArguments.AzureTransformArgumentsList)
                {
                    var p = new Program(rewriterToolArguments.IsMigration, azureTransformArguments.SourceDir, azureTransformArguments.DestDir, azureFileInformationCollection);
                    if (!p.CheckParameters())
                    {
                        continue;
                    }

                    var result = p.Rewrite();
                    if (result != 0)
                    {
                        exitCode = result;
                    }
                }
                return(exitCode);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
                return(1);
            }
        }
Beispiel #3
0
        private static int Main(string[] args)
        {
            try
            {
                var exitCode = 0;
                if (args.Length != 3 && args.Length != 4)
                {
                    PrintUsage();
                    return(1);
                }

                // Parse the basic arguments
                var rewriterToolArguments = ParseRewriterToolArgumentsFile(args[0], args[1], args[2]);
                if (rewriterToolArguments == null)
                {
                    return(1);
                }

                // Register logger
                var consoleLogListener = new ConsoleLogListener();
                var htmlLogFile        = Path.Combine(rewriterToolArguments.AzureTransformArgumentsList.First().SourceDir, "log", "log.html");
                if (File.Exists(htmlLogFile))
                {
                    File.Delete(htmlLogFile);
                }
                var htmlLogListener = new HtmlLogListener(htmlLogFile);
                Logger.RegisterListener(consoleLogListener);
                Logger.RegisterListener(htmlLogListener);

                // Parse advanced migration parameters
                AzureFileInformationCollection azureFileInformationCollection = new AzureFileInformationCollection();
                if (args.Length == 4)
                {
                    azureFileInformationCollection.AzureVideoInfoMapping = AzureVideoHelper.ParseAzureVideoFile(args[3], rewriterToolArguments.IsMigration);
                }

                if (rewriterToolArguments.IsMigration)
                {
                    GenerateAzureFileInfoForMigration(args[0], rewriterToolArguments, args[2], azureFileInformationCollection);
                }
                else
                {
                    GenerateAzureFileInfo(args[0], rewriterToolArguments, args[2], azureFileInformationCollection);
                }

                foreach (var azureTransformArguments in rewriterToolArguments.AzureTransformArgumentsList)
                {
                    var p = new Program(rewriterToolArguments.IsMigration, azureTransformArguments.SourceDir, azureTransformArguments.DestDir, azureFileInformationCollection);
                    if (!p.CheckParameters())
                    {
                        continue;
                    }

                    var result = p.Rewrite();
                    if (result != 0)
                    {
                        exitCode = result;
                    }
                }
                return(exitCode);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
                return(1);
            }
            finally
            {
                Logger.Flush();
                Logger.UnregisterAllListeners();
            }
        }
Beispiel #4
0
        private static bool GenerateAzureFileInfoForMigration(
            string repositoryRoot,
            RewriterToolArguments rewriterToolArguments,
            string azureDocumentUriPrefix,
            AzureFileInformationCollection azureFileInformationCollection)
        {
            var azureMarkdownFileInfoMapping        = new ConcurrentDictionary <string, AzureFileInfo>();
            var azureResourceFileInfoMapping        = new ConcurrentDictionary <string, AzureFileInfo>();
            var azureIncludeMarkdownFileInfoMapping = new ConcurrentDictionary <string, AzureFileInfo>();
            var azureIncludeResourceFileInfoMapping = new ConcurrentDictionary <string, AzureFileInfo>();

            bool hasDupliateMdFileName = false;
            var  files = Directory.GetFiles(repositoryRoot, "*", SearchOption.AllDirectories);

            Parallel.ForEach(
                files,
                new ParallelOptions {
                MaxDegreeOfParallelism = 8
            },
                file =>
            {
                var relativePath = PathUtility.MakeRelativePath(repositoryRoot, file);
                if (IsIgnoreFile(relativePath, rewriterToolArguments.IsMigration))
                {
                    return;
                }

                var filePath      = PathUtility.NormalizePath(file);
                var fileName      = Path.GetFileName(file);
                var azureFileInfo = new AzureFileInfo
                {
                    FileName = fileName,
                    FilePath = PathUtility.NormalizePath(file),
                    NeedTransformToAzureExternalLink = false,
                    UriPrefix = string.Empty
                };

                var isIncludeFile = filePath.Split(new[] { Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries)
                                    .Any(folder => folder.Equals("includes", StringComparison.OrdinalIgnoreCase));
                var isMarkdownFile = Path.GetExtension(relativePath).Equals(MarkdownExtension, StringComparison.OrdinalIgnoreCase);

                AzureFileInfo conflictFile = null;
                var isSucceed = true;
                if (!isIncludeFile && isMarkdownFile)
                {
                    isSucceed = azureMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo);
                    azureMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile);
                }
                else if (!isIncludeFile && !isMarkdownFile)
                {
                    // For resource file, even if has conflicts, we regards that as succeed
                    azureResourceFileInfoMapping.TryAdd(fileName, azureFileInfo);
                }
                else if (isIncludeFile && isMarkdownFile)
                {
                    isSucceed = azureIncludeMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo);
                    azureIncludeMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile);
                }
                else
                {
                    // For resource file, even if has conflicts, we regards that as succeed
                    azureIncludeResourceFileInfoMapping.TryAdd(fileName, azureFileInfo);
                }

                if (!isSucceed)
                {
                    hasDupliateMdFileName = true;
                    Logger.LogError($"Error: GenerateAzureFileInfo failed. File: {file} name confilicts with: {conflictFile?.FilePath}");
                }
            });

            azureFileInformationCollection.AzureMarkdownFileInfoMapping        = azureMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
            azureFileInformationCollection.AzureResourceFileInfoMapping        = azureResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
            azureFileInformationCollection.AzureIncludeMarkdownFileInfoMapping = azureIncludeMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
            azureFileInformationCollection.AzureIncludeResourceFileInfoMapping = azureIncludeResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);

            return(!hasDupliateMdFileName);
        }
Beispiel #5
0
        private static bool GenerateAzureFileInfo(
            string repositoryRoot,
            RewriterToolArguments rewriterToolArguments,
            string azureDocumentUriPrefix,
            AzureFileInformationCollection azureFileInformationCollection)
        {
            var azureMarkdownFileInfoMapping = new ConcurrentDictionary <string, AzureFileInfo>();
            var azureResourceFileInfoMapping = new ConcurrentDictionary <string, AzureFileInfo>();

            var files = Directory.GetFiles(repositoryRoot, "*", SearchOption.AllDirectories);

            Parallel.ForEach(
                files,
                new ParallelOptions {
                MaxDegreeOfParallelism = 8
            },
                file =>
            {
                var relativePath = PathUtility.MakeRelativePath(repositoryRoot, file);
                if (IsIgnoreFile(relativePath, rewriterToolArguments.IsMigration))
                {
                    return;
                }

                var isSucceed = true;
                var azureTransformArguments = rewriterToolArguments.AzureTransformArgumentsList.FirstOrDefault(a => PathUtility.IsPathUnderSpecificFolder(file, a.SourceDir));

                // By default, all the link should be transformed to external link with azure uri prefix
                // However, if we find that the file is under one of the folder that need to be transformed. Then the prefix uri should be docs but not auzre
                var needTransformToAzureExternalLink = true;
                var uriPrefix = azureDocumentUriPrefix;
                if (azureTransformArguments != null)
                {
                    needTransformToAzureExternalLink = false;
                    uriPrefix = azureTransformArguments.DocsHostUriPrefix;
                }

                var fileName      = Path.GetFileName(file);
                var azureFileInfo = new AzureFileInfo
                {
                    FileName = fileName,
                    FilePath = PathUtility.NormalizePath(file),
                    NeedTransformToAzureExternalLink = needTransformToAzureExternalLink,
                    UriPrefix = uriPrefix
                };

                AzureFileInfo conflictFile;
                var isMarkdownFile = Path.GetExtension(relativePath).Equals(MarkdownExtension, StringComparison.OrdinalIgnoreCase);
                if (isMarkdownFile)
                {
                    isSucceed = azureMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo);
                    azureMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile);
                }
                else
                {
                    isSucceed = azureResourceFileInfoMapping.TryAdd(fileName, azureFileInfo);
                    azureResourceFileInfoMapping.TryGetValue(fileName, out conflictFile);
                }

                if (!isSucceed)
                {
                    Console.WriteLine($"GenerateAzureFileInfo warning: can't insert file: {file}, confilicts with: {conflictFile?.FilePath}");
                }
            });

            azureFileInformationCollection.AzureMarkdownFileInfoMapping = azureMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
            azureFileInformationCollection.AzureResourceFileInfoMapping = azureResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
            return(true);
        }