Detailed Description
Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be embodied in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
An embodiment of the present invention provides a method for constructing a code, where the method is mainly applied to a server side, and as shown in fig. 1, the method mainly includes:
101. a build command is received that indicates to build the object code.
Wherein the build command is used to instruct to build the target code matching the target environment. After the developer develops the source code, the developer needs to run the source code in the development environment, the test environment and the online production environment in sequence, so that when the source code needs to be run in a certain environment, the developer can issue a construction command based on a server (for clarity, hereinafter referred to as a code construction server, namely a server provided with a code construction tool) so as to construct a code suitable for the environment based on the source code developed by each developer.
It should be noted that the code building server may be a server dedicated to building the object code, or may be a server having both a function of building the object code and a function of running the object code, that is, a server corresponding to the target environment.
102. And determining the target environment parameters according to the construction command.
The construction command may or may not carry target environment parameters for describing the target environment. When the construction command carries the target environment parameter, the code construction server can directly extract the target environment parameter from the construction command, and when the construction command does not carry the target environment parameter, the target environment parameter can be obtained in other modes, such as an IP network segment of the server corresponding to the target environment.
103. And acquiring a pre-stored configuration file and a source code which corresponds to the configuration file and comprises an environment parameter identifier.
The configuration file comprises at least one environment parameter, an environment parameter identifier corresponding to the environment parameter and configuration information corresponding to the environment parameter identifier. The environment parameters in the configuration file mainly include environment parameters (e.g., "default") for describing a development environment, environment parameters (e.g., "test") for describing a test environment, environment parameters (e.g., "prod") for describing an on-line production environment, and environment parameters for describing other environments. In addition, the environment parameter identifiers in the configuration information corresponding to different environment parameters are often the same, and the configuration information marked by the same environment parameter identifier is often different.
In practical applications, when an application program or a website is developed, the developed application program or website often needs to be divided into a plurality of functional modules, and the functional modules are developed by different developers respectively. After each developer writes the configuration file for which the developer is responsible and the source code corresponding to the configuration file and including the environment parameter identifier, the configuration file and the source code can be sent to the code construction server, so that the code construction server stores the configuration file and the source code to the local, and the target code can be obtained according to the construction instruction after the construction instruction is received subsequently. The configuration files written by each developer may be the same or different.
104. And determining an environmental parameter identifier corresponding to the target environmental parameter and configuration information corresponding to the environmental parameter identifier according to the matching result of the target environmental parameter and the environmental parameter.
After the code building server obtains the pre-stored configuration file, the target environment parameters can be respectively matched with the environment parameters in the configuration file, the environment parameters identical to the target environment parameters are obtained, and then the environment parameter identifications corresponding to the target environment parameters and the configuration information corresponding to each environment parameter identification are obtained from the configuration file.
After the code building server receives the building command and determines the target environment parameter, the environment parameter identifier corresponding to the target environment and the configuration information corresponding to the environment parameter identifier may be obtained from the configuration file sent by each terminal (computer used by the developer), so as to obtain the target code based on step 105 in the following.
105. And preprocessing the source code according to the environment parameter identification corresponding to the target environment parameter and the configuration information corresponding to the environment parameter identification to obtain the target code.
Wherein the preprocessing comprises converting the source code comprising the environment parameter identification into code comprising the configuration information matching the target environment parameter. After the environment parameter identifier corresponding to the target environment parameter and the configuration information corresponding to the environment parameter identifier are respectively obtained from each configuration file, the source code corresponding to each configuration file can be respectively preprocessed, and the code containing the configuration information and matched with the target environment parameter is obtained, so that the target codes under different functional modules are obtained.
It should be noted that, when the server in the target environment executes the target code, the target code under each functional module needs to be loaded first, and then each section of the target code needs to be executed in sequence according to the predetermined sequence, so that when there are many functional modules, it takes much loading time. Therefore, in order to reduce the subsequent loading time, after the code construction server obtains the object codes under each functional module, the object codes can be spliced according to a preset sequence, and the spliced codes are stored in one file, so that the file can be subsequently deployed to the server under the target environment to be executed only by loading once.
The method for constructing a code provided by the embodiment of the invention can receive a configuration file which is sent by a terminal and contains various environmental parameters, environmental parameter identifications corresponding to each environmental parameter and configuration information corresponding to the environmental parameter identifications, and a source code corresponding to the configuration file, when a code suitable for a certain environment needs to be constructed, a user can send a construction command based on a server, after the server receives the construction command, the target environmental parameter can be determined according to the construction command, then the environmental parameter identifications corresponding to the target environmental parameter and the configuration information corresponding to the environmental parameter identifications are obtained from the configuration file, and finally the source code containing the environmental parameter identifications is converted into the target code which is matched with the target environmental parameter and contains the configuration information according to the environmental parameter identifications and the configuration information corresponding to the environmental parameter identifications, the target code is deployed to the server corresponding to the environment for execution, so that the codes stored in the servers of various environments only contain the codes suitable for the current environment, and no codes in other environments exist, and the phenomenon that the internal information of the enterprise is leaked due to the fact that the codes in other environments are displayed to the user is avoided. In addition, since the source code written by the developer using the environment parameter identification can be applied to various environments, there is no phenomenon of modifying the source code, thereby realizing the function of minimally intruding the source code.
Further, according to the above method embodiment, another embodiment of the present invention further provides a method for constructing a code, as shown in fig. 2, the method mainly includes:
201. a build command is received that indicates to build the object code.
202. And determining the target environment parameters according to the construction command.
In step 102 of the above embodiment, the construction command may or may not carry the target environment parameter. Therefore, after receiving the build command, the specific implementation manner of the code build server determining the target environment parameter may be: judging whether the construction command carries an environmental parameter; if the construction command carries the environmental parameters, determining the environmental parameters carried in the construction command as target environmental parameters; if the construction command does not carry the environmental parameters, basic information of a server corresponding to the target environment is obtained, and the target environmental parameters are determined according to the basic information, wherein the basic information comprises an IP (Internet Protocol) address.
Specifically, the code building server may locally store the IP addresses (or IP network segments) of servers corresponding to each environment in the system and the corresponding relationship between other basic information and the environment parameters. When the target environment parameter does not exist in the construction command, the basic information of the server corresponding to the target environment can be acquired from the construction command, or the basic information of the current server is directly detected (the code construction server at this time is the server corresponding to the target environment), and then the target environment parameter is determined according to the corresponding relation between the basic information and the environment parameter.
203. And acquiring a pre-stored configuration file and a source code which corresponds to the configuration file and comprises an environment parameter identifier.
204. And determining an environmental parameter identifier corresponding to the target environmental parameter and configuration information corresponding to the environmental parameter identifier according to the matching result of the target environmental parameter and the environmental parameter.
Further, in practical applications, a developer may forget to set a certain environmental parameter, an environmental parameter identifier corresponding to the environmental parameter, and configuration information corresponding to the environmental parameter identifier in a configuration file, so that the configuration file lacks information. Based on the above problems, the embodiments of the present invention mainly provide the following two solutions:
the method comprises the following steps: and if the target environment parameter does not exist in the configuration file, acquiring an environment parameter identifier corresponding to the target environment parameter and configuration information corresponding to the environment parameter identifier from the default configuration file.
The default configuration file is a configuration file which is stored in the code building server locally and agreed by a developer before writing the source code, and when the subsequent developer writes the source code, the developer can write the source code directly according to the default configuration file or modify the default configuration file according to the requirement and then write the source code.
Since the default configuration file is generally the same as the configuration file sent by each terminal, when the configuration file sent by a certain terminal does not have the target environment parameter and the environment parameter identifier and the configuration information corresponding to the target environment parameter (for example, the developer mistakenly deletes the target environment parameter, the environment parameter identifier corresponding to the target environment parameter and the configuration information corresponding to the environment parameter identifier), the environment parameter identifier and the configuration information corresponding to the environment parameter identifier, which are recorded in the default configuration file and correspond to the target environment parameter, can be directly determined as the environment parameter identifier and the configuration information to be used.
However, when writing the source code, the user may not write based on the environment parameter identifier and the configuration information corresponding to the target environment parameter in the default configuration file, so that the environment parameter identifier and the configuration information obtained directly based on the default configuration file may be wrong, which may cause that obtaining the target code based on the environment parameter identifier and the corresponding configuration information is wrong, and further when the target environment is an online test environment, a risk of system failure may occur. Therefore, to avoid the above problem, the following method may be adopted:
the second method comprises the following steps: and if the target environment parameters do not exist in the configuration file, determining a preset warning code for indicating that the system is in a repair state as a target code.
205. And preprocessing the source code according to the environment parameter identification corresponding to the target environment parameter and the configuration information corresponding to the environment parameter identification to obtain the target code.
The specific implementation manner of preprocessing the source code may be: replacing the environment parameter identification corresponding to the target environment parameter recorded in the source code with the configuration information corresponding to the environment parameter identification; and determining other environment parameters recorded in the source code according to the target environment parameters, and deleting codes corresponding to the other environment parameters.
For example, if the target environment parameter is "test", that is, the test environment, the environment parameter identifier corresponding to the target environment parameter and the configuration information corresponding to the environment parameter identifier are { "DATAHOST": http:///×. the "configuration": true };
part of the source code is:
after the source code is preprocessed based on the environment parameter identifier corresponding to the target environment parameter and the configuration information corresponding to the environment parameter identifier, the obtained target code is:
in addition, the codes corresponding to other environment parameters may be deleted first, and then the replacement processing of the environment parameter identifier and the configuration information corresponding to the environment parameter identifier may be performed based on the remaining codes.
206. And after the target codes corresponding to the source codes are obtained, carrying out duplication elimination processing on the obtained target codes according to a preset duplication elimination rule to obtain final target codes.
In practical applications, a phenomenon that functional modules developed by two developers are duplicated may be caused for some reasons, so that each target code obtained by the code building server is duplicated, and a situation that the same function is executed twice subsequently occurs. In order to solve the above problems, in this step, after the target codes corresponding to the source codes sent by each terminal are obtained, the duplication elimination processing is performed on each target code according to a preset duplication elimination rule, and then the code after duplication elimination is deployed on the server corresponding to the target environment.
According to a preset deduplication rule, a specific implementation manner of performing deduplication processing on each obtained target code may be: carrying out duplicate removal processing on object codes with the same file names; or, carrying out duplication elimination processing on object codes with the same code content; or, the object codes with the same hash value are subjected to deduplication processing. The last two implementation modes can perform deduplication processing on files with different file names and the same file content, and the third mode is higher in efficiency than the second mode.
Further, in practical applications, when a developer refers to a source code of a page developed by another developer for page layout, a situation of reference misordering may occur, for example, a description of a sequence of red, orange, yellow and green is wrongly referred to as red, yellow, green and orange, so that a phenomenon that different layouts exist on the same page may occur. In order to avoid the above problems, the embodiments of the present invention provide the following improvements: detecting whether CSS (Cascading Style Sheets) used for describing different target codes of the same page are the same; if not, displaying error prompt information. Because the running sequence of the target code is a development environment, a test environment and an on-line production environment, when error prompt information is displayed in the process of running the target code in the development environment, developers can immediately modify the error prompt information, and therefore the risk of page disorder on line is avoided.
Further, according to the above method embodiment, another embodiment of the present invention further provides an apparatus for constructing a code, as shown in fig. 3, the apparatus mainly includes: a receiving unit 31, a determining unit 32, an obtaining unit 33 and a processing unit 34. Wherein,
a receiving unit 31 that receives a build command for instructing to build the object code;
a determination unit 32 that determines a target environmental parameter from the build command received by the reception unit 31;
the acquiring unit 33 acquires a pre-stored configuration file and a source code corresponding to the configuration file and including an environment parameter identifier, where the configuration file includes at least one environment parameter, a corresponding environment parameter identifier corresponding to the environment parameter, and configuration information corresponding to the environment parameter identifier;
a determining unit 32, configured to determine configuration information corresponding to the target environment parameter according to the target environment parameter and the matching result of the environment parameter;
and the processing unit 34 is configured to perform preprocessing on the source code according to the environment parameter identifier corresponding to the target environment parameter determined by the determining unit 32 and the configuration information corresponding to the environment parameter identifier, so as to obtain the target code, where the preprocessing includes converting the source code including the environment parameter identifier into a code including the configuration information, which is matched with the target environment parameter.
The device for constructing a code provided by the embodiment of the invention can receive a configuration file which is sent by a terminal and contains various environmental parameters, environmental parameter identifications corresponding to each environmental parameter and configuration information corresponding to the environmental parameter identifications, and a source code corresponding to the configuration file, when a code suitable for a certain environment needs to be constructed, a user can send a construction command based on a server, after the server receives the construction command, the target environmental parameter can be determined according to the construction command, then the environmental parameter identifications corresponding to the target environmental parameter and the configuration information corresponding to the environmental parameter identifications are obtained from the configuration file, and finally the source code containing the environmental parameter identifications is converted into the target code which is matched with the target environmental parameter and contains the configuration information according to the environmental parameter identifications and the configuration information corresponding to the environmental parameter identifications, the target code is deployed to the server corresponding to the environment for execution, so that the codes stored in the servers of various environments only contain the codes suitable for the current environment, and no codes in other environments exist, and the phenomenon that the internal information of the enterprise is leaked due to the fact that the codes in other environments are displayed to the user is avoided. In addition, since the source code written by the developer using the environment parameter identification can be applied to various environments, there is no phenomenon of modifying the source code, thereby realizing the function of minimally intruding the source code.
Further, as shown in fig. 4, the processing unit 34 includes:
the replacing module 341 replaces the environment parameter identifier corresponding to the target environment parameter recorded in the source code with the configuration information of the environment parameter identifier mark;
the determining module 342 determines that other environment parameters are recorded in the source code according to the target environment parameters;
the deleting module 343 deletes the codes corresponding to the other environment parameters determined by the determining module 342.
Further, as shown in fig. 4, the determination unit 32 includes:
the judging module 321 judges whether the construction command carries an environmental parameter;
a first determining module 322, configured to determine the environmental parameter carried in the construction command as the target environmental parameter when the judgment result of the judging module 321 is that the construction command carries the environmental parameter;
an obtaining module 323, configured to obtain basic information of the server corresponding to the target environment when the determination result of the determining module 321 is that the construction command does not carry the environment parameter;
the second determining module 324 determines the target environment parameter according to the basic information acquired by the acquiring module 323, where the basic information includes an internet protocol IP address.
Further, the obtaining unit 33 obtains, when there is no target environment parameter in the configuration file, an environment parameter identifier corresponding to the target environment parameter and configuration information corresponding to the environment parameter identifier from the default configuration file.
Further, the determining unit 32 determines a preset warning code indicating that the system is in a repair state as the target code when the target environment parameter is not in the configuration file.
Further, as shown in fig. 4, the apparatus further includes:
and the duplicate removal unit 35, after obtaining the object codes corresponding to the source codes, performs duplicate removal processing on the obtained object codes according to a preset duplicate removal rule to obtain final object codes.
Further, as shown in fig. 4, the deduplication unit 35 includes:
the first duplication removal module 351 is used for carrying out duplication removal processing on the object codes with the same file names;
the second deduplication module 352 performs deduplication processing on target codes with the same code content;
the third deduplication module 353 performs deduplication processing on object codes with the same hash value.
Further, as shown in fig. 4, the apparatus further includes:
the detecting unit 36 detects whether the cascading style sheets CSS for describing different target codes of the same page are the same;
and a display unit 37 for displaying error prompt information when the detection results of the detection units 36 are different.
In the foregoing embodiments, the descriptions of the respective embodiments have respective emphasis, and for parts that are not described in detail in a certain embodiment, reference may be made to related descriptions of other embodiments.
It will be appreciated that the relevant features of the method and apparatus described above are referred to one another. In addition, "first", "second", and the like in the above embodiments are for distinguishing the embodiments, and do not represent merits of the embodiments.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
The algorithms and displays presented herein are not inherently related to any particular computer, virtual machine, or other apparatus. Various general purpose systems may also be used with the teachings herein. The required structure for constructing such a system will be apparent from the description above. Moreover, the present invention is not directed to any particular programming language. It is appreciated that a variety of programming languages may be used to implement the teachings of the present invention as described herein, and any descriptions of specific languages are provided above to disclose the best mode of the invention.
In the description provided herein, numerous specific details are set forth. It is understood, however, that embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures and techniques have not been shown in detail in order not to obscure an understanding of this description.
Similarly, it should be appreciated that in the foregoing description of exemplary embodiments of the invention, various features of the invention are sometimes grouped together in a single embodiment, figure, or description thereof for the purpose of streamlining the disclosure and aiding in the understanding of one or more of the various inventive aspects. However, the disclosed method should not be interpreted as reflecting an intention that: that the invention as claimed requires more features than are expressly recited in each claim. Rather, as the following claims reflect, inventive aspects lie in less than all features of a single foregoing disclosed embodiment. Thus, the claims following the detailed description are hereby expressly incorporated into this detailed description, with each claim standing on its own as a separate embodiment of this invention.
Those skilled in the art will appreciate that the modules in the device in an embodiment may be adaptively changed and disposed in one or more devices different from the embodiment. The modules or units or components of the embodiments may be combined into one module or unit or component, and furthermore they may be divided into a plurality of sub-modules or sub-units or sub-components. All of the features disclosed in this specification (including any accompanying claims, abstract and drawings), and all of the processes or elements of any method or apparatus so disclosed, may be combined in any combination, except combinations where at least some of such features and/or processes or elements are mutually exclusive. Each feature disclosed in this specification (including any accompanying claims, abstract and drawings) may be replaced by alternative features serving the same, equivalent or similar purpose, unless expressly stated otherwise.
Furthermore, those skilled in the art will appreciate that while some embodiments described herein include some features included in other embodiments, rather than other features, combinations of features of different embodiments are meant to be within the scope of the invention and form different embodiments. For example, in the following claims, any of the claimed embodiments may be used in any combination.
The various component embodiments of the invention may be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. Those skilled in the art will appreciate that a microprocessor or Digital Signal Processor (DSP) may be used in practice to implement some or all of the functions of some or all of the components of the method and apparatus for constructing code in accordance with embodiments of the present invention. The present invention may also be embodied as apparatus or device programs (e.g., computer programs and computer program products) for performing a portion or all of the methods described herein. Such programs implementing the present invention may be stored on computer-readable media or may be in the form of one or more signals. Such a signal may be downloaded from an internet website or provided on a carrier signal or in any other form.
It should be noted that the above-mentioned embodiments illustrate rather than limit the invention, and that those skilled in the art will be able to design alternative embodiments without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses shall not be construed as limiting the claim. The word "comprising" does not exclude the presence of elements or steps not listed in a claim. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The invention may be implemented by means of hardware comprising several distinct elements, and by means of a suitably programmed computer. In the unit claims enumerating several means, several of these means may be embodied by one and the same item of hardware. The usage of the words first, second and third, etcetera do not indicate any ordering. These words may be interpreted as names.