Welcome to my blog

A few days ago I added a new entry to the history of this blog.

  • 23 April 2005 — I bought my noteslog.com domain name.
  • 4 December 2005 — I started this blog at he.net.
  • 6 April 2007 — I changed this blog to anhosting.com.
  • 11 April 2013 — I changed this blog to digitalocean.com, Ubuntu 12.04
  • 23 April 2014 — I was forced to let expire my noteslog.com domain name.
  • 24 May 2017 — I changed this blog to digitalocean.com, Ubuntu 16.04

In the meantime, it’s URL went through

  • http://mondotondo.com/aercolino/noteslog
  • http://noteslog.com
  • http://andowebsit.es/blog/noteslog.com
  • http://mondotondo.com

How to use Rake::PackageTask to create a zip file

I found out that the easiest way to build a WordPress plugin is using a rake task. In fact, you can use Rake::PackageTask to create a zip file. However, there is very little documentation about how to do it in practice. Here is my solution.

  1. (destroy and) create a folder to hold just the needed files
  2. copy the (possibly transformed) needed files to that folder
  3. compress the folder

Here is my Rakefile:

require 'rake/packagetask'

namespace :nzymes do
    desc 'Build plugin'
    task build: %w(destroy create package)

    desc 'Destroy dist folder and its contents'
    task :destroy do
      rm_rf 'dist'
    end

    desc 'Create dist folder and its contents'
    task :create do
      mkdir_p 'dist/nzymes/vendor'
      cp_r 'vendor/Ando', 'dist/nzymes/vendor'
      cp_r 'src', 'dist/nzymes'
      cp_r Dir.glob('*.php'), 'dist/nzymes'
      cp_r Dir.glob('*.md'), 'dist/nzymes'
      cp_r 'readme.txt', 'dist/nzymes'
    end

    Rake::PackageTask.new('nzymes', :noversion) do |p|
      p.need_zip = true
      p.package_dir = 'dist'
    end
end

Here are the tasks it creates:

andrea at Lock-and-Stock in ~/dev/wordpress/plugins/nzymes on master [!?$]
$ rake --tasks
rake nzymes:build            # Build plugin
rake nzymes:clobber_package  # Remove package products
rake nzymes:create           # Create dist folder and its contents
rake nzymes:destroy          # Destroy dist folder and its contents
rake nzymes:package          # Build all the packages
rake nzymes:repackage        # Force a rebuild of the package files

Here is how you can use it:

andrea at Lock-and-Stock in ~/dev/wordpress/plugins/nzymes on master [!?$]
$ rake nzymes:build
rm -rf dist
mkdir -p dist/nzymes/vendor
cp -r vendor/Ando dist/nzymes/vendor
cp -r src dist/nzymes
cp -r nzymes.php dist/nzymes
cp -r nzymes-manual.md README.md dist/nzymes
cp -r readme.txt dist/nzymes
cd dist
zip -r nzymes.zip nzymes
  adding: nzymes/ (stored 0%)
  adding: nzymes/nzymes-manual.md (deflated 70%)
  adding: nzymes/nzymes.php (deflated 46%)
  adding: nzymes/README.md (deflated 48%)
  adding: nzymes/readme.txt (deflated 53%)
  adding: nzymes/src/ (stored 0%)
  adding: nzymes/src/Nzymes/ (stored 0%)
  adding: nzymes/src/Nzymes/Capabilities.php (deflated 84%)
  adding: nzymes/src/Nzymes/Engine.php (deflated 80%)
  adding: nzymes/src/Nzymes/Options.php (deflated 73%)
  adding: nzymes/src/Nzymes/Plugin.php (deflated 74%)
  adding: nzymes/src/Nzymes/Stack.php (deflated 70%)
  adding: nzymes/src/Nzymes/Unused.php (deflated 69%)
  adding: nzymes/vendor/ (stored 0%)
  adding: nzymes/vendor/Ando/ (stored 0%)
  adding: nzymes/vendor/Ando/ErrorFactory.php (deflated 79%)
  adding: nzymes/vendor/Ando/Exception.php (deflated 29%)
  adding: nzymes/vendor/Ando/Regex.php (deflated 78%)
  adding: nzymes/vendor/Ando/StarFunc.php (deflated 73%)
cd -

Here is what you get:

andrea at Lock-and-Stock in ~/dev/wordpress/plugins/nzymes on master [$]
$ ls -la dist
total 104
drwxr-xr-x   4 andrea  staff    136 May 25 13:03 .
drwxr-xr-x@ 20 andrea  staff    680 May 25 13:03 ..
drwxr-xr-x   8 andrea  staff    272 May 25 13:03 nzymes
-rw-r--r--   1 andrea  staff  50814 May 25 13:03 nzymes.zip

How to replace text everywhere in Git

There is a Git command, filter-branch, which works wonders for changing the history of a repository, but it’s difficult to use because it forces you to know how Git works under the hood. Instead there is this little free tool, BFG Repo-Cleaner, which is intuitive, fast and recommended.

Check that there are some occurrences of your string

The first thing to do is to check that the repository contains the string you want to replace with another string. This will help later to make sure you effectively replaced it.

andrea at Lock-and-Stock in ~/dev/ruby
$ git clone git@gitlab.com:aercolino/your-repository.git

andrea at Lock-and-Stock in ~/dev/ruby
$ cd your-repository

andrea at Lock-and-Stock in ~/dev/ruby/your-repository
$ git log -G"your string" -i --all
commit ...
Author: ...
Date:   ...

    Message 2

commit ...
Author: ...
Date:   ...

    Message 1

Notice that, on the above git log line, the -G option allows to specify a regular expression, and the -i option allows to  ignore case.

Install BFG

BFG needs a JVM and you can install one with brew on a Mac.

$ brew cask install java
$ brew install bfg

Create search-replace.txt

BFG takes a file for specifying the text to search and replace. The accepted format is one replacement per line, like SEARCH==>REPLACE, with optional prefixes regex:, and glob:.

regex:your string==>another string

Use BFG

BFG acts on a bare repository which you can get by cloning with the --mirror option. Make sure the last commit doesn’t contains the text to be replaced, otherwise add a clean commit and push it before cloning.

andrea at Lock-and-Stock in ~/dev/ruby
$ git clone --mirror git@gitlab.com:aercolino/your-repository.git

andrea at Lock-and-Stock in ~/dev/ruby
$ bfg --replace-text search-replace.txt your-repository.git

andrea at Lock-and-Stock in ~/dev/ruby
$ cd your-repository.git

andrea at Lock-and-Stock in ~/dev/ruby/your-repository.git
$ git reflog expire --expire=now --all && git gc --prune=now --aggressive

Backup the old your-repository

You can recover this backup analogously to the section below.

andrea at Lock-and-Stock in ~/dev/ruby
$ git clone --mirror git@gitlab.com:aercolino/blog-experiment.git blog-experiment.git.backup

Create a new your-repository

This step involves removing your remote old repository and creating a remote new one with the same name as before. Then you can push to it from your local repository using the --mirror option.

andrea at Lock-and-Stock in ~/dev/ruby/your-repository.git
$ git push origin --mirror

Check that your string was replaced by another string

If all is OK then you should see no results when searching again for your string but at least the same number of results you got when looking for it before if you now look for the replacement string.

andrea at Lock-and-Stock in ~/dev/ruby
$ git clone git@gitlab.com:aercolino/your-repository.git

andrea at Lock-and-Stock in ~/dev/ruby
$ cd your-repository

andrea at Lock-and-Stock in ~/dev/ruby/your-repository
$ git log -G"your string" -i --all
(nothing shown)
andrea at Lock-and-Stock in ~/dev/ruby/your-repository
$ git log -G"another string" -i --all
commit ...
Author: ...
Date:   ...

    Message 2

commit ...
Author: ...
Date:   ...

    Message 1