pre-commit.com hook to reject commit if a file contains a specific string - pre-commit.com

Is there already a pre-commit.com hook to reject commit if a file contains a specific string?
I know how to do this with a git pre-commit hook: https://stackoverflow.com/a/26836281/633961
But I would like to go the pre-commit.com way because this would streamline my way to a way which is done by other developers.
For example I would like to use the same string which other people use, too.

The easiest way is to use a language: pygrep hook. This implements a regex matching againsta file
pygrep is especially useful for a repo: local hook which lives directly in your repository's .pre-commit-config.yaml file
an example which forbids DONTSHIP:
- repo: local
hooks:
- id: dontship
name: DONTSHIP check
entry: DONTSHIP
language: pygrep
types: [text]
disclaimer: I'm the creator of pre-commit

Related

Rmarkdown rticles - MDPI template. How to print your own abbreviations

I am using the MDPI template (from the rticles package), and keen to also use the glossaries packages so I don't have to manually feed all the abbreviation on the appropriate YAML field.
For such, I have loaded the LaTex package glossaries using the header-includes:
header-includes:
\usepackage{inputenc}
\usepackage[acronym, section=section]{glossaries}
\setacronymstyle{long-short}
\makeglossaries
\makeindex
\input{glossary}
after creating several acronyms within the Rmarkdown body, I would be willing to either input the latex commands and have it printed in within the "Abbreviations" section of the template.
Currently, I am able to hack it through the following steps (I am sure there is a better way):
1- keep all the aux files chunk with :
options(tinytex.clean = FALSE)
2- cmd makeglossaries "filename"
3- Raw Latex on Rmarkdown file:
\begin{abbreviations}
\setabbreviationstyle[acronym]{long-short}
\printglossary[type=\acronymtype,title={}]
\end{abbreviations}
However, I would be keen to know if I could insert something on the YAML and use the MDPI formatting.
Alternatively, I could edit the rticles MDPI template (but I am not sure how).
Any ideas?
Cheers,
Include in YAML:
include-after: glossary.tex
I tried without success, but it seems reasonable that this approach might work with a few modifications

PhpStorm: possible to update file template on each file edit?

My PhpStorm 2017.2 project requires that each new file be created from a specific. In "Settings >> Editor >> File and Code Templates >> PHP File", I have the following template:
<?php
/**
* #author John Doe
* #copyright ${YEAR} Acme
* #created ${DATE}
* #modified ${DATE}
*/
This works well. PhpStorm fills in the year and date dynamically. However, when I later come back and make changes to the file, I always need to remember to change the #modified line manually. Is there a way to automate this so that onSave or onCommit (for version controlled file), the line is updated with the current value of ${DATE}?
Not possible ATM.
https://youtrack.jetbrains.com/issue/IDEABKL-7178 -- watch this ticket (star/vote/comment) to get notified on any progress. Right now there are no plans to implement something like that in nearest future.
On another hand (as mentioned in the comment in aforementioned ticket) -- see if standard "Copyright" plugin will be of any help (never used it myself so no idea of what exactly it can do).
One possible solution involves writing your own script/program (PHP or whatever other language you can use) that will parse your file (regex matching should do fine here -- no real need into going and parsing file into tokens) and update such info:
look at each line until the matching line will be found (some guard logic can be added to limit the number of lines to be parsed: if no matching line is found in first xx (e.g. 20) lines then assume that this file has no such comment/line);
update date/time part based on file modification timestamp.
Once you have such script -- just use File Watcher functionality so it gets called on each file modification.
Possible downside: File Watcher gets triggered when file modification is detected ... which may include changes made outside (e.g. another editor/download from remote host/another VCS branch/etc)). This may lead to unnecessary/unwanted updates.
If File Watcher functionality is not suitable for whatever reason -- look into grunt -watch or alike where you may easily disable watching (so your script will only be called when your watcher (build runner) is watching).

Where to place presave functions?

I need to manipulate a content type (people) before being saved from ADMIN.
I need to save the name + surname in the title
I know the presave functions (hook_node_presave or hook_entity_presave) should be used.
My problem is where should these functions be written?
I am writing them in themes / custom / MYPROJECT / MYPROJECT.theme but they do not run
Where are the preset functions written for ADMIN to use instead of the normal ones?
You can put your hook in a .module file of a module. You should create a custom module for this, example below:
in modules/custom create a new folder {PROJECT_NAME}_general now referred to as MODULE_NAME) and add 2 files: {MODULE_NAME}.info.yml and {MODULE_NAME}.module
In the .info.yml file you just put some information regarding your custom module, for example:
name: Name of your module
type: module
description: Description of your custom module
core: 8.x
package: Custom
Now you can enable your module using either drush (drush en MODULE_NAME -y) or using "extend" from the admin menu.
Finally add your hook to the .module file and write the desired code.
You can also find all of this information here and here
Hope this helps you out!

Editing values during build via hooks

Im trying to edit a version variable when I build my applications, but I can't get any scripts to run using the cordova hooks at all.
I want to get the version from the package.json and the last 5 digits of the git commit so i can have something like 1.0.0.89gkt as my versions.
In the past for ionic 1 and using grunt or gulp I was able to add the scripts into the build process easily.
I've tried both the old way using the hooks/hook_name/script format and using the hook tag in the config.xml and neither work for me.
Overwriting the ionic scripts via the package.json allows me to change the scripts that are automatically run, but I want to avoid that if I can. Though I can easily add my text replace to one of the copy scripts or something (ill need to work out which one is best)
If someone knows a better way or a reason as to why the hooks wont fire, please let me know.
This is the solution that I ended up using.
scripts/before_prepare_increment_build_number.js
var fs = require('fs');
var git = require('git-rev-sync')
console.log('Incrementing Build Number');
var file = fs.readFileSync('www/build/main.js', 'utf8');
var str = git.short();
console.log('short', str)
var result = file.replace(/{{GITVERSIONSTRING}}/g, str);
fs.writeFileSync('www/build/main.js', result);
console.log('Incrementing Build Number Completed');
config.xml
<hook src="scripts/before_prepare_increment_build_number.js" type="before_prepare"/>
I needed to ensure that everything was happening synchronously otherwise the built in scripts would start copying before the strings had been replaced.
Currently its targeting the whole main.js that is generated by the default ionic-app-scripts so all comparisons and replacements can be added as required.
This solution uses the built in cordova hook before_prepare
Another solution that can be used to make it a bit more efficient is targeting the individual files as required and adding the script before the build/serve scripts in the package.json and have npm control and manage it.
Inside your package.json you could add a script to a prepare hook.
"scripts": {
"prepare": "node increment_build_number.js",
}

Gradle groovy plugin's test is not picking up test resources

Probably a stupid question.
I'm writing a unit test for my gradle plugin. I understand that gradle custom plugin has groovy plugin applied by default, so plugin/src/test/resources will be included by default as my test resources.
Facts:
Test class location: plugin/src/test/groovy/foo/bar/Test.groovy
Test resource location: plugin/src/test/resources/foo/bar/myfile
I'm trying to access myfile from Test.groovy via
new File(this.class.getResource(".").toURI())
When debugging the above code resolves to this directory and myfile is not in there.
plugin/build/classes/test/foo/bar
myfile can however be found at
plugin/build/resources/test/foo/bar/myfile
Question: How do I access myfile from Test.groovy? Is this a standard behavior from groovy plugin?
UPDATE
What I wanted to get in my original use case was the directory because I have multiple resources (it was lost in the translation to this SO question). It seems like the resource is resolved correctly when I specify myfile like what Peter says. So:
new File(this.class.getResource(".").toURI()) --> plugin/build/classes/test/foo/bar
new File(this.class.getResource("myfile").toURI()) --> plugin/build/resources/test/foo/bar/myfile
I was adopting this hack before finally fixing it with explicit resource names: https://code.google.com/p/android/issues/detail?id=64887#c13
Instead of ., use myfile. If that doesn't help, try getClass().classLoader.getResource("foo/bar/myfile").