No head, no tail - Project Development Notes: How to Develop Code Update Tools ?! (under)

zhaozj2021-02-16  60

(Connected to the upper article http://www.9cbs.net/develop/Article/16/16299.shtm)

Ø Code update replacement process

The process of updating the code is mainly to search the corresponding file directory, find all the target files, read the target file into the string, and process the target file according to the template file content. Here is a brief description:

1. Read the input template file, read the TemplatAg, read the corresponding data structure of each CodeSession;

2. Scan the input original file directory, scan, find the file that meets the replacement condition, call the code update template;

3. Read the file into the memory, find some of the contents of the templatAg, if no results are false, then call step 4, if the results are true, call the fifth step directly;

4. Read the content in the NewSource section in each CodeSession, call the method to modify the string content of the file.

Then call the step 7 directly;

5. Replace the TemplatAg content of the file string with an empty string;

6. Read the contents of the contents in the OldSource section in each CodeSession, and use the string.replace method to replace the OldSource content to newsource content;

7. Retrieve the file string to the textage content.

Below is a part of the code indicating this step:

// gerentorcode function, code generation and update call entry

Public void gerentorcode ()

{

// This function has previously read the template file into this.Templatestructure,

// therety.Templatestructure is a DataSet type

// ***************************************** //

// m_sztagstring is deposited in the templatetage

M_sztagstring = this.templatestructure.tables ["CODETEMPLATE"]. Rows [0] ["TemplateTAG"]. TOSTRING ();

// Traverse CodeSession

Foreach (DataRow Row In this.Templatestructure.tables "[" CODESession "]. ROWS)

{

// Remove the contents in the CodeSession section that should not be used, respectively

// put in different function properties strings

IF (Row ["Value"]. TSTRING () == "TRY")

{

// TRY part of the code to be worth entering

M_sztrystring = row ["newsource"]. TOSTRING ();

// TRY part previously valued old code

M_SzoldTryString = Row ["oldsource"]. TOSTRING ();

}

IF (Row ["Value"]. TOSTRING () == "catch")

{

// Catch part of the code to be entered

m_szcatchstring = row ["newsource"]. TOSTRING ();

// CATCH part of the old code value

Oldcatchstring = row ["ooldsource"]. TOSTRING ();

}

}

// Gerrentorcode function,

/ / Will perform code generation or code update of all Web service files under this path; M_SzWebServicePath;

}

// gerentorcode function, code generation and update call entry

Public void gerentorcode (String Szpath)

{

// Store the string of the file content

String szsourcecode;

/ / Generate the structure of the target file directory

String [] DirectoryEntries = System.io.directory.GetFileSystem "(szpath);

// Temporary string

String sztemp;

String szsztemp1;

Int nstart;

Streamwriter streamwriter;

StreamReader StreamReader;

FileStream SB;

String szreturntype;

// Traverse the target file directory

Foreach (String Path in DirectoryEntries)

{

// If it is still a directory, the loop calls this function.

IF (! Directory.exists (PATH))

{

/ / Judgment Whether Web Service's code file

IF (path.substring (path.length - 8, 8) == ".asmx.cs")

{

// Read the code file into SzsourceCode

StreamReader = New StreamReader (path, system.text.encoding.getencoding ("GB2312"));

szsourcecode = streamreader.readtoend ();

StreamReader.close ();

// Judgment whether it contains m_sztagstring; it is determined whether or not it has been implanted

IF (szsourcecode.indexof (m_sztagstring)! = -1)

{

// If you have already been implanted. Replace TemplattaG

// Replace TRY; replace CATCH

szsourcecode = szsourcecode.replace (m_sztagstring, string.empty);

Szsourcecode = szsourcecode.replace (m_szoldtring, m_sztrystring);

Szsourcecode = szsourcecode.replace (m_szoldcatchstring, m_szcatchstring);

}

Else

{

// Loop look up the method in WebServic; (I know the rules are all webservice methods "[WebMethod (" beginning)

INT nwebmethod = szsourcecode.indexof ("[webmedhod (");

INT nwebmethodend = -1;

While (NWebMethod! = -1)

{

// Call the method to get the location of [WebMethod "}" corresponding to the first "{"

Nwebmethodend = GetWebMethode (NWebMethod, szsourcecode);

// is to remove the entire Web Service method string to SZTEMP

Sztemp = szsourcecode.substring (NWebMethod, NWebMethode 1);

// Back up SZTEMP, prepare for the following replacement

SzTemp1 = SzTemp;

NStart = sztemp1.indexof ("{"); // Add TRY string after the first "{" number

Sztemp1 = sztemp1.insert (NStart 1, M_SZTRYSTRING);

/ / Add a Catch string before "}" corresponding to the first "{"

Sztemp1 = sztemp1.insert (sztemp1.length -1, m_szcatchstring);

// Replace the backup SZTEMP

Szsourcecode = szsourcecode.replace (sztemp, sztemp1);

Nwebmethod = szsourcecode.indexof ("[WebMethod (", NWebMethod 1);

}

}

// Rewote the file

SB = new filestream (path, filemode.create);

Streamwriter = New StreamWriter (SB, System.Text.Encoding.Getencoding ("GB2312");

/ / Add TemplattaG in the beginning of the file string, and put it on the identification, prepare for the next update

Streamwriter.write (m_sztagstring szsourcecode);

streamwriter.close ();

}

}

Else

{

// If it is a directory, recursive call this function

Gerentorcode (PATH);

}

}

}

Ø Code update output

The output update output is not special, just use the new file content string to generate a file to overwrite the previous file.

l Parts that need attention

Updating C # You can use some questions. Here is written here, so as to avoid the same bending path. The emergence of these issues is mainly related to the IDE environment. The environment of Visual Studio.NET will automatically process the joined code, which will cause the final code update to not succeed. Here are the problems and solutions that meet:

Ø Ide problem:

8. Problem Description

The generated code is after opening from the IDE, or after re-compiling. The code update tool invalidates the update of the code file.

9. Analysis of the causes

The IDE automatically formats the code. The format of the replaced code has been modified.

For example, the following code is injected within the function:

// ** The following code is the code generated, please do not modify **//

Where is invisible, it is intended to be the relationship between the alignment between the code.

But after this file is opened or compiled by IDE, the code may be modified by IDE to form the following:

// ** The following code is the code generated, please do not modify **//

Since our mechanism is required to find out the content of the contents of the content in Old Source, it will fail to update.

2 solution

There are many solutions that can be available here. I only provide one of my programs. My solution is very simple, copy the part of the IDE has been modified, put it in the OLD Source section. In this case, there is no failure when replacing.

For example, in the above example, I will copy out three from the IDE, replace the Old Source section in the template file. Thus, during the code update, the update of the code can be successful.

Of course, there can be other methods, such as ignore the code update; or directly find a method to update the code file with the synchronization template file. Or define two or more Oldsource sections in the template file corresponding to a NewSource section, and so on. There are a lot of methods, which will not be explained here. Here is to re-emphasize the purpose of code generation tools and code update tools. The purpose of making code generation tools or code update tools is actually very simple: through various means to improve the work efficiency of the development process. The rough code generation tool can only help increase a little development efficiency, and more detailed and comprehensive code generation tools may be able to enhance the efficiency of the development process from a deep level. However, these are just a tool. There is no deepest combination with the entire developer, development tool, and the project we have developed, thereby forming a complete set of development processes, and it is possible to truly improve the productivity of development work.

When I will write a project next week, the problem encountered in this stage is much more than the problem that I have encountered more than before. Many friends have warned that my problem has appeared. I want to say some, and There are some problems that occur.

转载请注明原文地址:https://www.9cbs.com/read-24197.html

New Post(0)