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:

{[ .rakefile | =ruby= | 1.hl(2) ]}

Here are the tasks it creates:

{[ .tasks | 1.hl(1) ]}

Here is how you can use it:

{[ .build | 1.hl(1) ]}

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.

{[ .some-occurrences | 1.hl(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.

{[ .install-bfg | 1.hl(1) ]}

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:.

{[ .search-replace | 1.hl(1) ]}

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.

{[ .use-bfg | 1.hl(1) ]}

Backup the old your-repository

You can recover this backup analogously to the section below.

{[ .backup | 1.hl(1) ]}

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.

{[ .push-mirror | 1.hl(1) ]}

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.

{[ .no-occurrences | 1.hl(1) ]}

{[ .same-occurrences | 1.hl(1) ]}

 

How to install WordPress with Docker on OS Sierra

With individual Docker commands

  1. Install Docker for Mac
  2. $ docker run –name mysql-latest -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
  3. $ docker run –name wordpress -p 8000:80 –link mysql-latest:mysql -d wordpress:4.7.3-php7.1-apache
  4. Visit http://0.0.0.0:8000 and configure WordPress

With a Docker compose file

  1. Install Docker for Mac
  2. Save the following text to a ´~/dev /wordpress /website /docker-compose.yml´ file: {[ .=docker-compose.yml= | == | 1.hl(2) ]}
  3. Save the following text to a ´~/dev /wordpress /website /docker /wordpress /Dockerfile´ file {[ .Dockerfile | == | 1.hl(2) ]}
  4. Run everything: ´$ docker-compose up´
  5. Visit http://0.0.0.0:8000 and configure WordPress

Connect to the running dbms server from SequelPro

  1. Select the Standard tab
  2. Configure: {[ .sequel-pro | == | 1.hl(2) ]}
  3. Connect

Connect to a running server from a terminal

Example relative to connecting to the running dbms server.

  1. Find out the name of the server: {[ .=docker-compose ps= | == | 1.hl(2) ]}
  2. Connect to the ´website_dbms_1´ server: {[ .=docker exec= | == | 1.hl(2) ]}
  3. Then, for example, find out the server IP: {[ .hostname | == | 1.hl(2) ]}

How canActivate works for multiple guards

Angular 2 lets developers forbid routes activation by means of a list of guards assigned to the canActivate property of a route definition.

{[ .route-def | =javascript= | 1.hl(2) ]}

Those guards are classes which implement the CanActivate interface, and the implementation can be a promise, for example when it entails a roundtrip to the server.

The way Angular 2 treats this list of guards is by applying .every(...) to the values of a merge-mapped list of observables.

{[ .check-guards | =javascript= | 1.hl(2) ]}

The list of guards is conceptually the same as a list of promises, and in fact all the guards could be promises because, as we see above, .from(...) is used to initialise check$.

Merge-mapping is exactly what we did in How to convert promises to an observable.

{[ .merge-map | =javascript= | 1.hl(2) ]}

If we now apply .every(...) to that merge-mapped list of promises, we can see how canActivate works for multiple guards.

{[ .every | =javascript= | 1.hl(2) ]}

Notice that the subscription is notified only once, as soon as the resolved promises make it clear what the result would be: the observable emits a false soon after getting the first false, or a true after getting all true-s. So, as expected, canActivate works as a short-circuit-ed AND of each guard of the list, according to the arrival order of values.

Also notice that the values of all the promises keep arriving as promises get resolved, even after the observable has completed. This means that guards intended to be used together in a list of guards should never have side effects (to avoid race conditions). If guard G1 had a side effect of activating route R1, and G2 had another for R2, then canActivate: [G1, G2] would unpredictably activate R1 or R2, depending on which guard completes later.

How to convert promises to an observable

A promise is a future value. An observable is a flow of past and future values. So it makes sense to convert a list of promises into an observable. Then we can do nice things on it, like .every(...), which will result in a single boolean observable according to whether all the promises satisfy a given condition or not.

Understanding observables requires some time, plus the latest RxJs library, plus an interactive dev tool like JS Bin.

I’m now going to share what I just learned. But first, let me introduce some snippets I’ll be using later on.

Creating promises that resolve.

{[ .resolvePromise | =javascript= |  1.hl(2) ]}

Subscribing to a source.

{[ .subscription | =javascript= | 1.hl(2) ]}

Getting to the right observable

Starting with no promises at all

Let’s see how the source and subscription work for the simplest case, when no promises at all are involved.

{[ .no-promises | =javascript= | 1.hl(2) ]}

Notice that above and in the next snippets I’m going to show the console output corresponding to the subscription defined earlier and using the last source.

Passing from values to promises of values

With a simple .map(...) we can convert our booleans to promises.

{[ .promises | =javascript= | 1.hl(2) ]}

Notice that we are now getting reversed logs because, to get a better sense of promises, we are creating them so that they will resolve in reversed order.

Passing from promises to observable: 1/3

Unfortunately the .from(...) applied to a list of promises doesn’t really do much:

{[ .no-mapping | =javascript= | 1.hl(2) ]}

Notice that the subscription is notified all at once with the pending promises, without waiting for them to (slowly) resolve.

Passing from promises to observable: 2/3

We could transform each to an observable, applying.from(...) to each.

{[ .nothing-useful | =javascript= | 1.hl(2) ]}

Notice that the only difference is that now the subscription is notified with the pending observables, still without waiting.

Passing from promises to observable: 3/3

Wait a moment… that is an observable of observables… a higher-order observable then (by definition)!

Yes, and there are many ways to bring a higher order back to the first one.

combineAll

Converts a higher-order Observable into a first-order Observable by waiting for the outer Observable to complete, then applying combineLatest.

{[ .higher-order-combine-all | =javascript= | 1.hl(2) ]}

Notice how the subscription is notified only once, after all the promises have resolved, with a combination that respects the order of the booleans.

concatAll

Converts a higher-order Observable into a first-order Observable by concatenating the inner Observables in order.

{[ .higher-order-concat-all | =javascript= | 1.hl(2) ]}

Notice how the subscription is notified once per resolved promise, but only after all the promises have resolved. Also notice that the notification order respects the order of the booleans.

exhaust

Converts a higher-order Observable into a first-order Observable by dropping inner Observables while the previous inner Observable has not yet completed.

{[ .higher-order-exhaust | =javascript= | 1.hl(2) ]}

Notice how the subscription is notified only once, with the resolved value of the first promise (i.e. the first boolean here, not the first promise to resolve, which would be the last boolean). That’s because the first boolean is associated to the longer running promise, which exhausts the processing capacity of the resulting observable until it gets resolved (and completed). Also notice that the fact that the notification happens only after all the promises have resolved, is coincidental (because we made it happen by forcing each promise to complete sooner than the previous one).

mergeAll

mergeAll(concurrent: number): Observable

Converts a higher-order Observable into a first-order Observable which concurrently delivers all values that are emitted on the inner Observables.

{[ .higher-order-merge-all | =javascript= | 1.hl(2) ]}

Notice how the subscription is notified once per resolved promise, as soon as each promise is resolved.

switch

Converts a higher-order Observable into a first-order Observable by subscribing to only the most recently emitted of those inner Observables.

{[ .higher-order-switch | =javascript= | 1.hl(2) ]}

Notice how the subscription is notified only once, as soon as the first promise is resolved.

References

© 2017 Notes Log

Theme by Anders NorenUp ↑