📜 ⬆️ ⬇️

JScriptInclude Gear v 0.1.0 is a mechanism for cascading import of scripts / libraries. (Revenge)

Good day, dear habrazhiteli.

I offer you a post-rematch, called JScriptInclude Gear, a mechanism for cascading import of scripts / libraries. The previous post, the presenter of JScriptInclude Gear , is now selflessly taken up in drafts, now it is not relevant at all, despite the fact that the previous presentation was rather harshly perceived by Habra-javascript, and there it was for that :), a lot of disgraces were caused misunderstanding of the essence, namely the fact that
JScript (WSH)! = JavaScript (browsers) , in connection with which JScriptInclude Gear was raised to the rank of crutches, bicycles, “it was already”, “in-trainer”, etc.
I really wanted to share my decision then, but I had no idea how to make it “tasty” and understandable, but it seems to me that I have now succeeded.
Nevertheless, the “Obscuren” case got a move, people write, people use, people like it. Over the past week I received a lot of feedback, which stimulated me to work on my decision, in the end it was transformed beyond recognition. Thanks to everyone who came out with me to the line of fire.

But those who do not know what the actual speech is about are a small, enticing preface:
JScriptInclude Gear is a mechanism for cascading import of scripts / libraries designed for use in automation scripts written in JScript interpreted by WSH (Windows Script Host) technology in the environment of the Windows operating system family. Additionally providing functionality for organizing scripts in the form of basic modules, supporting their definition in configuration files and their subsequent calling by aliases, and so on and so forth :)

Those who have already read about JScriptInclude Gear in the previous post, in spite of maybe the remaining negative, I will ask you to stay behind, I assure you - there will be something interesting here, because almost everything has changed: the mechanism is now exported by the COM component and connects to your script with two lines of code, an SFX package has been prepared for installing the mechanism into the system with automatic registration of components and automatic configuration of the necessary paths, the documentation has been reworked "from and to", and is now presented in the form of HTML, the example now starts without “Dances with a tambourine”, and as a pleasant bonus, it contains several useful modules, which are described in the documentation (they, nevertheless, are an example of use and “do not pretend”), and the code itself is naturally “podylizan”, although in the process You don't actually use it now.
For those who love it, you are welcome on GitHub :
Mechanism JScriptInclude Gear SFX installation package.
(independent to ugliness :))
The mechanism of JScriptInclude Gear is as it is.
(About deployment in additional comments to the last commit, or in the help section jsImport Object sidebar "Attention !!!")
And yet I would like you to read the remaining "many bukaf", at least to the point Features JScriptInclude Gear :
')
Interested mercy I ask under habrakat ...


Assigning JScriptInclude Gear:


First of all, to compensate for the lack of technology in the ability to connect scripts / libraries as such. Windows Scripting technology suggests partially resolving this issue by wrapping code in the XML format of a specific WSF file (Windows Script File) , but its use does not allow for a " cascade of connections ." Due to this circumstance, it is impossible to form the structure of libraries with their own dependencies. In this solution, you are invited to overcome this problem by providing a cascade import .
Cascade import is an opportunity worthy of special attention - its principle was borrowed by the author from the Node.JS technology. In such a model, a set of JavaScript code files make up the flexible and uncluttered structure of modules and their dependencies on each other.
In Node.JS, such a mechanism is provided by the core of the technology, and it also provides control, in the case of JScriptInclude Gear — the mechanism is naturally imitation, and is implemented by means of the JScript language and some related technologies.

JScriptInclude Gear implementation:


Compared with the first editions of JScriptInclude Gear, they suggested that the user place the mechanism code at the beginning of the target script, and this is not less than 110 lines of code , in this edition the author overcomes this inconvenience using the Windows Script Components technology, which allows, in a special way, described in XML - format, scripts register as COM components . Thus, the author prepared the JScriptInclude.wsc component, the main purpose of which is to export the source code of the mechanism.
The author took into account the inconvenience associated with the need for some manual configuration of the paths, and overcome this circumstance by preparing a special SFX package , which, in addition to providing the necessary files, also registers the above-mentioned component in the system as " JScriptIncludeGear ", and calls the script to automatically execute the necessary updates . source paths of the mechanism.
As for the obligatory use of the godless eval () for initializing the imported code, and now for initializing the code of the mechanism itself in the global stack, this problem is insurmountable , since in WSH there is no possibility, in any way, to execute code in the context of a global object, except by calling it through eval () from the main stack of the executable script.
PS: Let me remind you once again that JScript (WSH)! = JavaScript (in browsers), despite the fact that both are implementations of the ECMA 262 standard, they exist in completely different environments.
There is no window in WSH , there is no DOM , and others like script tags, and so on and so forth.

After successful installation in any of your JScript scripts on the target system, you can connect the JScriptInclude Gear cascade import mechanism , as well as access the system of its basic modules as follows:
jsIncludeGear = new ActiveXObject("JScriptIncludeGear"); eval(jsIncludeGear.getSourceCode()); 

The component mentioned by the author is intended only for exporting the source code of the mechanism itself by calling the getSourceCode () method, initializing the result of its execution through eval () you can access the global jsImport object, through the properties and methods of which the main functionality of the mechanism is implemented.
For example, the main import / declaration dependency instruction:
 jsImport.require(targetScript) 

The jsImprot object, its methods and properties are covered in a separate section of the reference tool supplied with the JScriptInculde Gear mechanism. (After successful installation from the SFX-package, you will be asked to familiarize yourself with it, as well as its location will be shown in a message)

Also in the target directory of the installation of the JSciprInclude Gear mechanism is the \\ example \\ directory, in it, in the TestDir directory you will find the JScriptExample.js file , which is an example of using the mechanism, it demonstrates the main features and the concept proposed by the author. (After successful installation from the SFX-package, you will be asked to start it, as well as the directory containing the example opens in the explorer)

The final instruction of any import procedure should be to initialize the loaded code, as follows:
 eval(jsImport.initialization()); 



JScriptInclude Gear features:


1) Connecting an arbitrary script - given the possibility of a simple design
 jsImport.require(targetScript) 

connect another script to the executable, execute its code, or access the entities declared in it.
This allows you to reuse existing practices and turnkey solutions in their work.
2) Cascade import - it is possible to connect scripts / libraries, which, in turn, also have the opportunity to indicate the need to connect other scripts / libraries (the author suggests to call this later " dependencies ", and their fact of their connection as " dependency implementation " ).
The dependency designation in the connected scripts / modules is implemented by including the same connection instruction in the code:
 jsImport.require(targetScript) 

i.e. the same challenge as the one with which they were connected.
In general, the connection is based on the principle of a downward recursive call to the import procedure. In this mechanism, the use of absolute and relative paths in determining dependency is applicable. It is worth emphasizing that relative paths, when declaring a dependency, are considered on the basis of the location of the script in which this dependency is declared, i.e. in a connected script, relative to this actual connected script , and not relative to the initiator script (the script in which the mechanism is declared, and the first import is performed in a cascade).

3) Connection Singularity - This mechanism, during the import process, keeps track of already connected files and does not allow them to be reinitialized, even if the link to the same script / library is present in a set of dependencies along the cascade branching path.

4) Providing a model of standard modules - This mechanism provides the means for organizing the structure of basic modules when scripts / libraries are standardized or universal and are assumed to be present in the system. To do this, JScriptInclude Gear uses the configuration files of the basic modules describing them by aliases, through which later any script that connects the mechanism can use all of its basic modules. The toolkit includes methods for defining new and redefining existing modules, loading custom configurations:
Definition of a new module with a special instruction. In the case of a cascade of connections, it makes it possible to use the declared module throughout the underlying part of the cascade, as well as in all subsequent import procedures:
 jsImport.defineBaseModule(moduleName, modulePath) 

, the configuration file itself " jsModulesConfig.js ", by default is the root directory of the JScriptInclude Gear installation, during installation it automatically detects modules from the base distribution. If it is necessary to add / change / replace any modules, you can:
- edit the base configuration file, which is not recommended because for example, in the case of an update, this file is likely to be changed.
- use custom configuration files, and during the execution of your code connect them with special instructions:
 jsImport.getConfig(ConfigFilePath) 

Warning The modules declared in the user configuration file, if the aliases coincide with the aliases of the modules declared in the basic configuration, will prevail over them and overlap them.

5) Caching imported code for debugging or reuse - In the process of developing this mechanism, the question of debugging imported code was repeatedly raised. Due to the fact that it is initialized through eval () , it is not possible to receive a correct error message in the loaded code - the link to the line of code that caused the exception is not informative about your script. To resolve this problem, caching of the imported code is provided, i.e. its materialization through writing to a separate CacheFile.js script file . The need for writing is determined by the establishment of a special property:

 jsImport.WriteCache = true 


Later, through its launch, it is possible to get the correct error message.
PS: The cache file can be used as a compiled solution, i.e. later use it instead of the new import procedure; in this case, the goal of using JSscriptInclude Gear is not clear, although the author sees that such a solution may be useful, if only by means of simplifying the assembly of code from multiple files.

About all a little more dear, in the attached documentation.

And once more ... Yes, please!


Mechanism JScriptInclude Gear SFX installation package.
(independent to ugliness)
The mechanism of JScriptInclude Gear is as it is.
(About deployment in additional comments to the last commit, or in the help section jsImport Object sidebar "Attention !!!")

UPD: For some users, the SFX package does not register components.
SFX is a bit redone, now it seems to work, but again not everyone, for example, there are problems on x64, the main suspects so far are antiviruses.
If, after installing the example, it was not possible to start the example immediately, then register JScriptInclude.wsc manually,
Or run the AfterInstall.js script yourself .

Source: https://habr.com/ru/post/148416/


All Articles