As the Drush API evolves, more and more modules are developped to profit from that API, which varies over time. We need to be able to detect under which environment we're running. Right now, modules have to resort to hacks such as checking the available commandline options and so on, which is really just a hack (see #662586: Not compatible with latest drush HEAD: Drush version 2.1 or higher is required for drush make to work. for how it can break).

We could have a "drush_api_version()" function that would return the current version number, which would need to be bumped at each release. Alternatively, it could even be easier to provide just a constant:

--- a/drush.php
+++ b/drush.php
@@ -22,6 +22,7 @@ if (version_compare(phpversion(), DRUSH_MINIMUM_PHP) < 0) {
 
 define('DRUSH_BASE_PATH', dirname(__FILE__));
 
+define('DRUSH_API_VERSION', '2.1');
 
 define('DRUSH_REQUEST_TIME', microtime(TRUE));
 

I think this should go in before the 3.x API stabilises, so I'm marking this critical.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

Agreed - we discussed this at #425670-11: drush self update ... Lets use DRUSH_VERSION constant.

anarcat’s picture

Status: Active » Needs work

I think it should be in a .inc file so it can be included without side effects.

anarcat’s picture

Status: Needs work » Needs review
FileSize
305 bytes

Here's a tentative patch.

greg.1.anderson’s picture

If you want to avoid side effects, maybe it should be stored in a non-php file.

version.txt:

DRUSH_VERSION = 2.1

or:

version.txt:

2.1

Or perhaps even:

drush.info:

name = drush
version = "2.1"

A module .info file would have version = 7.x-2.1 or something like that; I'm not sure what the format would be for drush, which is independent of the Drupal version.

anarcat’s picture

Well, .inc files should only define functions so they should be "parseable". I meant we should use .inc instead of drush.php (which actually executes code) as I proposed originally.

Not sure we should get into the 7.x-2.1 mess: that would just cloud things. Then again, if tarballs are generated by drupal.org and .info files are used, then we don't have to maintain version in multiple places. The downside is that there is no API documentation unless you get drush from a tarball...

moshe weitzman’s picture

a constant in drush.inc looks good to me ... I think it should currently read 3.0-dev, for better consistency with drupal's system.module pattern.

greg.1.anderson’s picture

Status: Needs review » Needs work

I didn't see your patch -- I was typing.

A .inc file does have a side effect of sorts; if you include one more than once, you will get a 'function already defined error' for the defined functions in the file. require_once will help, but won't save you if two different .inc files include the same function.

Suppose you're trying to do a drush self update, and you want to know what version the to-be-installed package is. If you want to always trust the uri / filename, then you're good with drush.inc. If you want to be able to unpack the target and verify its version, then a .txt or .info file is better.

I could quite easily agree that it's fine to trust the filename, but there's one more thing to discuss before committing the patch as submitted, and that is how to version HEAD. It seems that define('DRUSH_VERSION', '2.1'); should only appear in a stable tagged release of drush. drush-HEAD should instead have something like define('DRUSH_VERSION', '3.x-HEAD');.

I'm not exactly sure if 3.x-HEAD is correct, but in any event, whatever is committed for HEAD should be appropriate for HEAD, naturally.

greg.1.anderson’s picture

There I go typing again.

+1 for the patch in #3 with the text "3.0-dev".

anarcat’s picture

@greg - not sure I follow here. :) From what you're saying here, I conclude that the best approach would be to have the version in a separate file. I'm less and less sure about the .info file because that's a file that can be broken too easily by Drupal.org.

Otherwise, what do we want to document here? There are two different requirements: the API version (for interoperability) and the drush version (for self upgrades and such).

The API should be considered to be stable on a given branch, i.e. x.y should be API stable for a constant x (that's not currently the case with the 2.x branch). Once we start having a stable release branch, we only need one constant. I would of course prefer that, but that's possible only starting with x > 2.

So basically, right now, the tree is with the moving 2.x API. Once 3.0 is released (or more precisely once the API gets frozen, which may be when 3.0-rc1 is released), the API version is 3, and then the drush version numbers are 3.0, 3.1, etc. Then HEAD becomes 4.x-dev and doesn't have a stable API anymore. But now I'm crossing over to #682644: Provide a stable release branch.

So bottomline here:

1. the DRUSH_VERSION now is 2.x-dev for HEAD code, it will hit 3.x-dev once we start doing 3.x alpha/beta releases and is considered stable and frozen once an RC is pushed out. Third party modules should have to rely only on the first digit for API checks, unless DRUSH_VERSION is < 3.
2. the DRUSH_VERSION constant should be put in a new file to avoid side effects (like defining functions). Whether it's an info file or php file needs to be discussed.

Can we start from there? :)

greg.1.anderson’s picture

Status: Needs work » Needs review
FileSize
2.49 KB

How about this.

I used the same format as a .info file to store the version, but I named the file "drush.txt" instead of "drush.info" to avoid the risk that some code might confuse drush with a drupal module. (Edit: I notice from #425670: drush self update that "drush.info" is going to be created when drupal.org packages up drush, so that filename is definitely out.) I'm not sure that "drush.txt" is the best name; any better suggestions? "drush.version" might be nice, although eventually other stuff could go in this file.

My comments on my opinions on what the version should be are in #682644: Provide a stable release branch, but at the end of the day, I think that the current drush version is whatever Moshe says it is, so I put in "3.0-dev" per #6 above (which is pretty much what I would pick anyway).

anarcat’s picture

Looks nice! I'm not sure i like the idea of a .info file named .txt, especially since that could be confused with documentation. Furthermore, things will get even more confusing once Drupal.org kicks in and generate its own .info file that will conflict with this.

Can't we just make that file drush.info and let drupal.org overwrite it? What would be the problem in that case? We can still read it and parse the version number that Drupal.org generates. We avoid duplicated information and things are way more coherent overall.

I'm also keeping the version number bikeshed to #682644: Provide a stable release branch. ;)

greg.1.anderson’s picture

FileSize
2.5 KB

I agree, and in fact, this morning it struck me that "drush.info" is the right filename to use. This file should be checked into cvs with the current "dev" version. When someone makes a cvs tag to create a release, the Drupal packaging system will overwrite drush.info with a file that will contain the right version number.

At least, that's what I thought would happen; the current 2.1 tarball does not contain a drush.info file. I still think that drush.info is the way to go, but if anyone can shed any light on the packaging system (which I've read a little about, but have never used), I'd appreciate it.

Here's a better patch.

anarcat’s picture

Maybe the .info file doesn't get created if it already exists?

I also agree .info is the way to go. Maybe we could fleshen it a bit so it contains the fields required in the specification?

I am thinking of:

; $Id$
name = Drush
description = "Drush (the drupal shell) provides a command line interface for Drupal."
version = 3.0-dev
core = 7.x

Note that I include core = just because it's required in the spec. We could actually not include the line at all, but then we would be at risk of drupal.org refusing to edit the file (unsure).

Basically, we would end up restoring this file: http://drupalcode.org/viewvc/drupal/contributions/modules/drush/drush.in... ... but adding a version line.

greg.1.anderson’s picture

Maybe the .info file doesn't get created if it already exists?

I'm thinking the opposite; since there is no .info file in 2.1, and there is no .info file in the current 2.1 tarball...

Your suggestions look good to me, but since I have no practical experience with packaging, I must defer to others judgement in this matter.

anarcat’s picture

Crap, I again said the opposite of what I thought: "maybe the .info file doesn't get created *unless* it already exists?" sorry for the confusion.

I think the patch should just be tested and committed already. :)

moshe weitzman’s picture

Lets add version info to status command and not do own command for this. When we need to get version info out programmatically, we can add a --pipe option for status. Otherwise, looks good.

greg.1.anderson’s picture

Status: Needs review » Needs work

How about drush status foo as an equivalent for drush status | grep '^foo' | sed -e 's/^foo://'? (That's approximate, not tested / working). I know you usually don't want to add overhead to drush where grep will suffice, but it seems that the utility would be high enough in this instance to make it worthwhile. More and more stuff is getting accumulated in status...

moshe weitzman’s picture

Hmm. Maybe. This is a bit like wanting to control the columns that get emitted as we have discussed for statusmodules.

In this case, I think a --pipe which emits CSV or .ini or something is a bit more consistent.

anarcat’s picture

Here drush status gives me:

$ ./drush.php status
Could not find a valid Drupal installation

If we put the version number in there, we won't be able to find out the version number without a valid drupal install. I would favour a separate option instead. We could also have the version info in status in addition to the new option though...

greg.1.anderson’s picture

Status: Needs work » Needs review
FileSize
5.6 KB

Here is a patch with "Drush Version" included in status. Status will now print the drush version instead of the error message if it cannot find a valid drupal installation. An "ini-file-like" output is produced with "drush --pipe status".

The "drush version" command is gone. Must say I was mighty partial to it, though. Oh well.

alias dv="./drush status --pipe | grep 'drush_version' | sed -e 's/.*=//'"

:)

moshe weitzman’s picture

OK, I just re-read your suggestion from #17. I like it too. Lets add it. You could rip out the new --pipe stuff if you think its not needed anymore.

I guess drush.txt is for me to author. OK, I can handle that. Will just put version in there for now.

greg.1.anderson’s picture

No, it should be drush.info -- and I just forgot to cvs add it, so it didn't show up in the diff. I'll do the addition from #17, cvs add drush.info and then post a new patch.

I think I'll leave the --pipe stuff there, unless someone wants me to get rid of it.

greg.1.anderson’s picture

FileSize
7.16 KB

Okay, here it is again, this time with drush.info included.

$drush status version
  Drush Version     : 3.0-dev
  Drupal Version    : 6.14
$drush status drush-version
  Drush Version     : 3.0-dev

I think that --pipe is still very useful, so I left it in.

moshe weitzman’s picture

I thought we were going to provide a way to get just the value without the label. So, one of these should return only 3.0-dev:

$drush status drush-version
$drush status drush-version --pipe
greg.1.anderson’s picture

FileSize
6.87 KB

No problem at all. Here you go.

moshe weitzman’s picture

OK, very close. Sorry to keep nitpicking this ...

- document --pipe option in command help. perhaps in 'examples' as well
- move drush version and php config to the end of the since those are likely not what the user is after.
- is there some reason we don't use drush_print_table() instead of these aligned colons? I think long values would wrap better. I asked similar question at http://drupal.org/node/679832#comment-2468256 but got a slightly unsatisfying reply. I know that this issue isn't about presentation, but while we're here ...

greg.1.anderson’s picture

FileSize
8.17 KB

Edit: Stuff an nonsense; the italics below make no sense. Skip that part.

I'm going to echo the slightly unsatisfying reply again here; it is a pain to make an array of the kind that drush_print_table wants. drush status is providing an array of key : value pairs, but drush_print_table wants an array of rows containing an array of columns. Awkward. Rather than try to adjust the array prior to processing, I "fixed" drush_print_table so that it could also accept an array of key : value pairs, which it interprets as a two-column table. This is okay, but I think I like the aligned colons better, both from an output and coding standards viewpoint.

I added the documentation and reordered the output as suggested; I also switched the output from cvs to space-delimited to match what 'help' does. (Similarly, I did not document --pipe in 'options' because 'help' does not do this, and --pipe is already documented in the global options). However, I must say that I remain in doubt about the values-only output format. drush status --pipe now reads like some sort of obscure Jeopardy question; in contrast to drush help --pipe, I cannot imagine how the output of drush status --pipe would ever be used in a script. I think that the ini-format should come back. I toyed briefly with the thought of printing only the value if there is only one item in the output (e.g. drush status drush-version --pipe), or revert to ini-format if there are multiple lines. This would usually be what you wanted, but you could get into trouble if you called drush status --pipe expecting ini-format, but got back only "3.0-dev" because Drupal didn't bootstrap and there were no other values in the table. For this reason I hesitate, but it might still be the best / simplest option.

You can commit this patch if you like it, but I am ready to roll another version to adjust some of this stuff if you want. Take a look, see how it feels and let me know what you think.

greg.1.anderson’s picture

FileSize
8.44 KB

Here is a new patch that uses drush_print_table via a better construct that still preserves the useful key : value format of the status table array. I still like the aligned colons better, but this at least removes the bad taste from patch G.

I also modified drush status --pipe to print either a single value or an ini-format list depending on the size of the result set. This is almost always just what you want, but perhaps it is a bit too magical.

Try it on and see how it feels. I'll roll a hybrid of H and G if you like, or something different.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

I like using drush_print_table because of its superior wrapping of long values. I added a long Lorem ipsum to the end of our new drush version value and it wrapped perfectly.

I'm fine with committing this as is. If you prefer the colons, then I have a proposal; rewrite drush_key_value_to_array_table() as a wrapper around drush_print_table(). In the new wrapper function, prepend a colon to each value.

jonhattan’s picture

about presentation and my unsatisfying response: a few days later I propose a new function `drush_sprint` with signature drush_sprint($key, $value, $indent = 2, $width = 18, $separator = ": ").

Basicly $output .= drush_sprint($key, $value); do the same as `drush status`'s old way $output .= sprintf(" %-18s: %s\n", $key, $value);. It also does custom wrapping (no php function) that has worked pretty well in my tests.

Patch and the resulting output at 80 columns at #679832-7: pm-info: show info for one or more projects.

greg.1.anderson’s picture

Status: Reviewed & tested by the community » Fixed

If you prepend a ':', then the lines won't wrap right. Inserting an extra column between the key and the value (containing a ':', of course) solved the problem perfectly, though.

Committed. Please go ahead and re-open if the behavior of --pipe bugs you.

(The solution is always out there.)

moshe weitzman’s picture

Status: Fixed » Active

I hadn't thought of adding a column. Nice one.

--pipe behavior makes sense to me. i think we should add another help example that does `drush st drush_version --pipe` so folks know they can get a raw value.

greg.1.anderson’s picture

Status: Active » Fixed

Committed. Also fixed the help text that was incorrect (still applied to patch in #27).

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.