Microsoft.VisualStudio.Project.TokenProcessor.UntokenFile C# (CSharp) Méthode

UntokenFile() private méthode

private UntokenFile ( string source, string destination ) : void
source string
destination string
Résultat void
        public virtual void UntokenFile(string source, string destination)
        {
            if(string.IsNullOrEmpty(source))
                throw new ArgumentNullException("source");

            if(string.IsNullOrEmpty(destination))
                throw new ArgumentNullException("destination");

            // Make sure that the destination folder exists.
            string destinationFolder = Path.GetDirectoryName(destination);
            if(!Directory.Exists(destinationFolder))
            {
                Directory.CreateDirectory(destinationFolder);
            }

            //Open the file. Check to see if the File is binary or text.
            // NOTE: This is not correct because GetBinaryType will return true
            // only if the file is executable, not if it is a dll, a library or
            // any other type of binary file.

            uint binaryType;
            if(!NativeMethods.GetBinaryType(source, out binaryType))
            {
                Encoding encoding = Encoding.Default;
                string buffer = null;
                // Create the reader to get the text. Note that we will default to ASCII as
                // encoding if the file does not contains a different signature.
                using(StreamReader reader = new StreamReader(source, Encoding.ASCII, true))
                {
                    // Get the content of the file.
                    buffer = reader.ReadToEnd();
                    // Detect the encoding of the source file. Note that we
                    // can get the encoding only after a read operation is
                    // performed on the file.
                    encoding = reader.CurrentEncoding;
                }
                foreach(object pair in tokenlist)
                {
                    DeleteToken deleteToken = pair as DeleteToken;
                    if (deleteToken != null)
                    {
                        DeleteTokens(ref buffer, deleteToken);
                        continue;
                    }

                    ReplaceBetweenPairToken replaceBetween = pair as ReplaceBetweenPairToken;
                    if (replaceBetween != null)
                    {
                        ReplaceBetweenTokens(ref buffer, replaceBetween);
                        continue;
                    }

                    ReplacePairToken replacePair = pair as ReplacePairToken;
                    if (replacePair != null)
                    {
                        ReplaceTokens(ref buffer, replacePair);
                        continue;
                    }
                }
                File.WriteAllText(destination, buffer, encoding);
            }
            else
                File.Copy(source, destination);
        }

Usage Example

Exemple #1
0
        /// <summary> 
        /// Add Bar Descriptor to each project. 
        /// </summary>
        private void AddBarDescriptor()
        {
            try
            {
                DTE dte = _applicationObject as DTE;
                Projects projs = dte.Solution.Projects;

                List<Project> projList = new List<Project>();
                foreach (Project proj in projs)
                {
                    projList.Add(proj);
                }

                while (projList.Count > 0)
                {
                    Project proj = projList.ElementAt(0);
                    projList.RemoveAt(0);

                    Configuration config;
                    Property prop;
                    try
                    {
                        config = proj.ConfigurationManager.ActiveConfiguration;
                        prop = config.Properties.Item("ConfigurationType");
                    }
                    catch
                    {
                        config = null;
                        prop = null;
                    }

                    if (prop == null)
                    {
                        if (proj.ProjectItems != null)
                        {
                            foreach (ProjectItem projItem in proj.ProjectItems)
                            {
                                if (projItem.SubProject != null)
                                    projList.Add(projItem.SubProject);
                            }
                        }
                        continue;
                    }

                    if (Convert.ToInt16(prop.Value) != Convert.ToInt16(ConfigurationTypes.typeApplication))
                        continue;

                    if (config.PlatformName != BLACKBERRY && config.PlatformName != BLACKBERRYSIMULATOR)
                        continue;

                    ProjectItem baritem = proj.ProjectItems.Item(BAR_DESCRIPTOR);
                    string n = proj.Name;
                    if (baritem == null)
                    {
                        tokenProcessor = new TokenProcessor();
                        Debug.WriteLine("Add bar descriptor file to the project");
                        string templatePath = dte.Solution.ProjectItemsTemplatePath(proj.Kind);
                        templatePath += BAR_DESCRIPTOR_PATH + BAR_DESCRIPTOR;
                        tokenProcessor.AddReplace(@"[!output PROJECT_NAME]", proj.Name);
                        string destination = System.IO.Path.GetFileName(templatePath);

                        // Remove directory used in previous versions of this plug-in.
                        string folder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(proj.FullName), proj.Name + "_barDescriptor");
                        if (Directory.Exists(folder))
                        {
                            try
                            {
                                Directory.Delete(folder);
                            }
                            catch (Exception e)
                            {
                            }
                        }

                        folder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(proj.FullName), "BlackBerry-" + proj.Name);
                        System.IO.Directory.CreateDirectory(folder);
                        destination = System.IO.Path.Combine(folder, destination);
                        tokenProcessor.UntokenFile(templatePath, destination);
                        ProjectItem projectitem = proj.ProjectItems.AddFromFile(destination);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
All Usage Examples Of Microsoft.VisualStudio.Project.TokenProcessor::UntokenFile