kscript : How to get directory of current file? - kotlin

Is there a way to get directory of a current script location in kotlin script?
I could achieve this in bash with
dirname $0
or
# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`

The builtin kotlin-main-kts script definition adds a __FILE__ variable to the script context. Access works just as you'd expect:
println(__FILE__) // java.io.File object representing /home/user/bin/foo.main.kts (or wherever the script is)
println(__FILE__.parentFile) // java.io.File object representing /home/user/bin
println(__FILE__.absolutePath) // the string /home/user/bin/foo.main.kts
println(__FILE__.parent) // the string /home/user/bin
You can also change the name of the variable using the annotation ScriptFileLocation:
#file:ScriptFileLocation("scriptPath")
println(scriptPath.absolutePath) // /home/user/bin/foo.main.kts
Keep in mind IntelliJ autocomplete doesn't love changing the variable name like that, though. :-)
So how do you run your script with kotlin-main-kts? In Kotlin 1.3.70 and above, it's as easy as ensuring your script's name ends in .main.kts. The compiler will automatically apply this special context, giving you access to __FILE__. For more information, check out the Kotlin repository here.

You can use:
File(".")
and don't forget to import java.io.File

Related

Can we pass a command line argument to property file reader in jmeter

I have a config.property file that contains all the property values to be used in jmeter, so i am using property file reader plugin to read the property file, here the problem is i don't want to hard code the path to config.properties file in property file reader so i want it to pass as command line argument but it is not working
command i am executing is
.\jmeter -JPROPERTY_FILE=<file_location> -n -t <path_to_jmx> -l <path_to_jtl> -j <path_to_log>
In the File Path of Property File Reader, replace:
${PROPERTY_FILE}
By using __P function:
${__P(PROPERTY_FILE)}
Your mistake is that you’re using Variable syntax for a property.
See:
http://jmeter.apache.org/usermanual/functions.html#__P
http://jmeter.apache.org/usermanual/functions.html#functions
You should be using __P() function like ${__P(PROPERTY_FILE)} or even __property() function like ${__property(PROPERTY_FILE,PROPERTY_FILE)}. The latter one automatically stores the retrieved value into a JMeter Variable so you won't have to additionally declare it under User Defined Variables of the Test Plan
Instead of using custom plugins I would suggest going for built-in JMeter functionality, there is -q command-line argument which allows loading and arbitrary .properties file so you will not have to install the plugin, care about order of Configuration Elements, etc.

Current file path in Live Template

Is it possible to get the full path of the current file within a live template in IntelliJ? I've tried using groovyScript("new File('.').absolutePath") function, but that returns /Applications/IntelliJ IDEA.app/Contents/bin/. and not the file path as I was hoping for.
Thanks!
According to the docs (emphasis mine):
You can use groovyScript macro with multiple arguments. The first argument is a script text that is executed or a path to the file that contains a script. The next arguments are bound to _1, _2, _3, ..._n variables that are available inside your script. Also, _editor variable is available inside the script. This variable is bound to the current editor.
The _editor is an instance of EditorImpl which holds a reference to the VirtualFile that represents the currently opened file.
Therefore, the following script gets the full path of currently opened file.
groovyScript("_editor.getVirtualFile().getPath()")
Or if you want to get the path relative to the project's root:
groovyScript("_editor.getVirtualFile().getPath().replace(_editor.getProject().getBaseDir().getPath(), \"\")")
Since IntelliJ IDEA 2019.3 the Live Template macros filePath() and fileRelativePath() are available. A complicated Groovy script macro is no longer required.

Could not import module frege.system.Directory (java.lang.ClassNotFoundException: frege.system.Directory)

I tried to import System.Directory in my Frege program (In Eclipse) in order to use functions as getDirectoryContent, etc., and it writes me this error :
Could not import module frege.system.Directory (java.lang.ClassNotFoundException: frege.system.Directory)
What do I have to do ?
It is because the module frege.system.Directory doesn't exist in Frege. A good way to find out about a module is to use Hoogle for Frege at this URL: http://hoogle.haskell.org:8081. If we search for that module there, we can see that it doesn't list any module as opposed to, say, if you search for frege.data.List, we would see the module in the result.
Now for the functions you need like getDirectoryContent, if you look at the search result for frege.system.Directory, the first result is about processes and the third and fourth results are about jars and zip files. If you click on the second result, it would open the module frege.java.IO and you can see some relevant functions that might be useful for you (list for example). However the Haskell module you are trying to find is not yet ported to Frege but it should, of course, be possible to port that module backed by native Java implementations.
Update for OP's comment
Here is a simple snippet to return the files under a given directory:
ls :: String -> IO [String]
ls dir = do
contents <- File.new dir >>= _.list
maybe (return []) (JArray.fold (flip (:)) []) contents
Regarding createTempFile, the following works for me:
frege> File.createTempFile "test.txt"
String -> STMutable RealWorld File

Check if Windows batch variable starts with a specific string

How can I find out (with Windows a batch command), if, for example, a variable starts with ABC?
I know that I can search for variables if I know the whole content (if "%variable%"=="abc"), but I want that it only looks after the beginning.
I also need it to find out where the batch file is located, so if there is a other command that reveals the file's location, please let me know.
Use the variable substring syntax:
IF "%variable:~0,3%"=="ABC" [...]
If you need the path to the batch file without the batch file name, you can use the variable:
%~dp0
Syntax for this is explained in the help for the for command, although this variable syntax extends beyond just the for command syntax.
to find batch file location use %0 (gives full patch to current batch file) or %CD% variable which gives local directory

How do I change the default *.elog log file name for an interpreted Specman session?

I want to be able to specify the file name stem for the log file in a Specman test. I need to hard-code the main *.elog filename so that I don't get variance between tests and confuse the post-processing scripts. Is there a constraint or command line I can pass into Specman?
You can also use specman command "set log " or use following code.
extend sys
{
run() is also {
specman("set log specman.elog");
};
};
You can control the *.elog filename with switch -log.