How to add pre-commit git hooks to check that README.Rmd and index.Rmd have been knitted? - r

I have an R package with a pkgdown documentation site. I want to create a git hook so that if I try to commit and push changes to either README.Rmd or index.Rmd without first knitting them to create the corresponding .Md files, I'm warned. Right now I just forget.
The book R Packages says to use usethis::use_readme_rmd() to create the README, which will also create the git hook. But I already have a README.Rmd file.
How can I create a hook for an existing .Rmd file generally, whether it's README.Rmd or the index.Rmd from my pkgdown site? I'd like to use the usethis package but if it's simpler to do it outside of that package, I'm open to that.

A different approach is to do this with Github Actions if that is where your pkgdown site is.
Create the folder .github within your repo
Within that create the folder workflows
Within that create the file render-readme.yml
Paste this code in that file
on:
push:
paths:
- README.Rmd
- Index.Rmd
name: Render README and Index
jobs:
render:
name: Render README and Index
runs-on: macOS-latest
steps:
- uses: actions/checkout#v2
- uses: r-lib/actions/setup-r#v1
- uses: r-lib/actions/setup-pandoc#v1
- name: Install packages
run: Rscript -e 'install.packages(c("rmarkdown", "knitr"))'
- name: Render README
run: Rscript -e 'rmarkdown::render("README.Rmd", output_format = "md_document")'
- name: Render Index
run: Rscript -e 'rmarkdown::render("Index.Rmd", output_format = "md_document")'
- name: Commit results
run: |
git commit README.md -m 'Re-build README.Rmd' || echo "No changes to commit"
git commit Index.md -m 'Re-build Index.Rmd' || echo "No changes to commit"
git push origin || echo "No changes to commit"
Push this to GitHub and it should start working immediately. Note it takes a bit of time to process. Click on the Actions tab in your GitHub repo to see the progress.
See https://github.com/r-lib/actions for examples. Code above is adapted from that.
Note you might want to divide the action into 2 files. render-readme.yml and render-index.yml. That way if the Action fails, you'll know which file has the problem.

Related

Serve RMarkdown outputs without version controlling them

We frequently use RMarkdown based packages to create websites with R (bookdown, blogdown, distill...) and use github-pages to serve the html files via the url username.github.io/repo.
In this approach, the ouput (i.e. html / css) files are also version controlled, and are frequently included in commits by mistake (git commit -a). This is annoying since these files clutter the commit and often lead to fictitious files conflicts.
Ideally, the outputfiles would not be version controlled at all, since the binary files (images) additionally bloat the repo. So I'm looking for a solution where:
Git ignores the output files completely but provides an alternative (but comparable1) method to gh-pages to serve them
Git ignores the output files temporally and committing / pushing them to gh-pages is done in a separate, explicit command
1: The method should be command line based and provide a nice URL to access the website
You could have .html, .css etc. ignored in the main and all other branches but the branch, for example, the gh-page branch, where your github-page is built from.
Git does not support different .ignore files in different branches so you would have to set up a bash script that replaces the ignore file each time you checkout a new branch. See here for how to do that: https://gist.github.com/wizioo/c89847c7894ede628071
Maybe not the elegant solution you were hoping for but it should work.
If you have a python installation on your computer, you can use GitHub Pages Import, a tool designed specifically for this purpose.
You need a python installation since it has to be installed with pip, but once it's installed it integrates beautifully with into an R / RMarkdown workflow.
Once it's installed (pip install ghp-import), you can run ghp-import docs (assuming docs is where your RMarkdown outputs are stored).
There are a bunch of practical options that you can use, including -p to additionally push the changes to your remote after the commit.
You need to tell Git to ignore the folder the book gets built into.
So, for example, by default bookdown puts all the built files in a folder called "_book"
Just add the following line to the .gitignore file in your project.
_book
Then you can work on your book and build it and push changes without worrying about the site being updated.
To update the site, you want to create a gh-pages branch that is only used for the hosted content. Do that with these commands from in your book folder:
git checkout --orphan gh-pages
git rm -rf .
# create a hidden file .nojekyll
touch .nojekyll
git add .nojekyll
git commit -m"Initial commit"
git push origin gh-pages
Make sure (once you put your book content in that branch) that GitHub is set to use that branch for hosting, rather than the folder you were using before.
Then, you can switch back to your main branch with the following command:
git checkout master
Next, you will clone your gh-pages branch into your main project:
git clone -b gh-pages https://github.com/yourprojecturl.git book-output
Then, when you have a version of the built book (in the _book folder) ready to use as your live site, use the following commands to copy the content into the book-output folder and push that to the gh-pages branch where the live site is:
cd book-output
git rm -rf *
cp -r ../_book/* ./
git add --all *
git commit -m"Update the book"
git push -q origin gh-pages
You can continue to use this last set of commands whenever you have a version in _book that you're ready to push live.

RStudio README.Rmd and README.md should be both staged use 'git commit --no-verify' to override this check

I am using RStudio, where I have both a README.Rmd and a README.md file. However, when I have only changed in the README.Rmd and want to commit and push it to GIT I get this:
RStudio README.Rmd and README.md should be both staged use 'git commit --no-verify' to override this check
Where should I add: "git commit --no-verify"?
And/or how can I avoid this message?
When you're editing your README.Rmd file, your README.md file is not automatically synchronized. Since GitHub will display your README.md (and not your README.Rmd file), there is a check that you have build your README.md file before pushing it to GitHub. Not doing so would prevent any change that you made in the README.Rmd file to appear on your repository.
I would suggest to always use the following workflow :
Edit your README.Rmd file
Build your README.md file by running devtools::build_readme() in the R console
Commit both your README.Rmd and README.md
Doing this should not throw any warning and everything will work the way you probably want.
People who bump into this question may want to check out the debate in the relevant issue of the usethis package.
In brief, this is caused by using usethis::use_readme_rmd(), which sets up a pre-commit hook in .git/hooks/pre-commit. This hook ensures that readme.Rmd and readme.md are updated at the same time. If only one changes, then the error message included in the title of this question appears.
This is likely to be annoying if, for example, your readme includes some summary statistics that can be rebuilt as the project updates or some random data.
The temporary solution is to do as the message says, i.e. go to terminal and type:
git commit --no-verify -m "my commit message"
If you want to get rid of this behaviour altogether, you should delete the .git/hooks/pre-commit file.
From the R console, you can achieve this with:
file.remove(".git/hooks/pre-commit")

Is it possible to generate artifacts without publishing with Semantic-release?

I'm currently using semantic-release for versioning of my react library.
https://github.com/semantic-release/semantic-release
Question:
Is it possible to generate the artifacts without publishing it?
For example, in my use case I would like to generate:
- Version Release Number (#semantic-release/commit-analyzer)
- tar file that will be publish to npm (#semantic-release/npm)
- change log (#semantic-release/release-notes-generator)
If you run the dry run option, it will print the version release number and the change log to console, but I want to store it to a file. One workaround is I could pipe the results and then parse, but it'll be nice if it can pass the plugin could put the data in a file during the dry run.
The dry run won't run the publish stage which is where the files get tar'ed up.
Any Advice appreciated,
Thanks,
Derek
You can use the npmPublish option of the #semantic-release/npm plugin. This will generate the tar file for the npm package but it won't publish it to the npm registry.
// In your package.json file add the following property which ensures that npm will not publish
"private": "true"
// In your github action's workflow/release.yml file use the following to store your tar file
- run: |
mkdir -p ~/new/artifact
echo ${{contents_of_your_file}} > ~/new/artifact/yourtarfile
- uses: actions/upload-artifact#v2
with:
name: artifactname
path: '~/new/**/*'
- name: download
uses: actions/download-artifact#v1
with:
name: artifactname
path: '~/new/**/*'

Spec rpm: how to enter in build directory without using tar files?

I want to build a project from git, and create the rpm,this is the spec,is incomplete, but the most interesting part is first one.
I need to enter in build dir created by git,but fail
This is the part of spec. file
%define build_timestamp %(date +"%Y%m%d")
#
Summary: My git project
Name: gitproject
Release: 1
Version: %{build_timestamp}
License: GPLv2+
Group: Unspecified
URL: https://mygitserver.com/myname/myproject
#-----------------------------------------------------
Requires: openssl
BuildRequires: compat-openssl10-devel
BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
#-----------------------------------------------------
AutoReqProv: no
%description
Git personal project, is in testing
%prep
git clone https://mygitserver.com/myname %{name}
cd %{name}
The problem is..instead of enter in %{name} enter in BUILD..which of course contain a lot of dirs..but not the makefile.
+ umask 022
+ cd /home/user/rpmbuild/BUILD
+ make -j2
make: *** No targets specified and no makefile found. Stop.
How can I enter %{name}?
I wouldn't skip the tgz part, but let git do the work for you. The way I solve this: use Source and %setup -q in your spec file:
Source: %{name}-%{version}.tgz
%prep
%setup -q
now you can use git to create this tgz file for you:
git archive --format=tgz --prefix=gitproject-1.2.3-1 <hash or tag> > /home/user/rpmbuild/SOURCES/gitproject-1.2.3.tgz
note that the prefix ang filename need to match the %name-%version.tgz from the Source tag in your spec file. You can change those naming conventions of course.
PS: I have write a whole convenience script around this; but with these commands you should get most of the way.

XCode 'Run Script' step to create directory and copy file is failing

I have the following custom script step during my build:
mkdir -p "${CONTENTS_FOLDER_PATH}/Frameworks"
cp "${SRCROOT}/testing.1.dylib" "${CONTENTS_FOLDER_PATH}/Frameworks"
The script runs successfully, but when I check the bundle the Frameworks directory does not exist.
Should not not work as I expect? (Frameworks folder created with the testing.1.dylib in it).
Edit: Added screenshot of the runscript.
How about trying the following:
dst=${CONFIGURATION_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/Frameworks
mkdir -p "${dst}"
cp "..." "$dst"
(I found your example and adapted it as above to copy a dylib into the 'Frameworks' folder of my framework).

Resources