Discourse Theme CLI (console app to help you build themes)

Wasn’t sure if I should ask this question here, or somewhere related to Theme Creator, but I’m unclear on how to get an API key for Theme Creator so I can use the Theme CLI tool with Theme Creator. Any guidance would be appreciated.

It’s already answered in How to create an api key on the admin panel. If you needed a user specific API key then you have to generate it in the URL /admin/users/USER_ID/USERNAME.

5 Likes

Thanks for the reply. I’m wondering about getting an API key specifically for https://theme-creator.discourse.org so that I can use the Theme CLI tool with Theme Creator. I’m not an admin of Theme Creator obviously, so not sure if this is feasible.

Hit “Edit Locally”:

Then generate an API key in the dialog that appears:

9 Likes

I just pushed a major update for this tool. The key changes are

  • Interactive user interface - this makes it much easier to get set up

  • API keys are stored per-site, instead of per-theme. So if you regularly use the same site, this will save a lot of time

  • If your API key changes, just add --reset to the command - no more manual editing of the config file

  • A new command discourse_theme download foldername allows you to download a theme from a live Discourse site, and then allows you to immediately start editing it

  • Improved error messages

  • The scaffold generated by discourse_theme new now includes the file/folder for theme translations

Here’s a quick demo video of the new interface and download command:

This should all work if you’re running the latest version of Discourse. If not, you will see a warning, and some errors for the download command.

These changes should all be compatible with macOS, Windows and Linux, but as always feel free to post bugs/comments/ideas below. If you have an old version of the tool installed, just run gem update discourse_theme.

Note that some of these features are not yet compatible with theme-creator.discourse.org. That should be remedied next week.

Version 0.2.1 of discourse_theme CLI is now fully compatible with the live version of theme-creator.discourse.org.

16 Likes

Here’s something I’d love to see – the ability to sync theme code both ways between a development instance and a folder.

There are times where it’s easier to make a change in the Discourse GUI, and I think it would be neat if those changes could sync if the watch option was running instead of copy/pasting code back and forth.

It may not be a trivial deal to implement, but just throwing an idea (not even sure if it’s a good one) out!

7 Likes

Cool idea! As a workaround for now you can use the theme CLI to “download” a theme from Discourse. So if you make a change in the Discourse GUI, stop “watching”, run a download, then start watching again.

5 Likes

I wondered if it did that but I didn’t want to test it on the active project I was working on! :laughing:

That’s great to know!

Normally I use git to grab the most recent theme, but today I was grabbing them via discourse_theme download and noticed that the first time I requested the theme, it didn’t work… but if I waited a few seconds and requested it again, it was working…

You just get an error 500 on the CLI tool, so this is an excerpt from the rails/production log… First time it shows “UndefinedConversionError” second time it shows “Sent data theme…tar.gz”

Server is running current version of tests-passed ( v2.3.0.beta5 +80 )

grabbing my theme…

Started GET "/admin/customize/themes/74/export?api_key=[FILTERED]" for 115.79.198.131 at 2019-03-19 08:51:19 +0000
Processing by Admin::ThemesController#export as */*
  Parameters: {"api_key"=>"[FILTERED]", "id"=>"74"}
Completed 500 Internal Server Error in 668ms (ActiveRecord: 10.0ms)
Encoding::UndefinedConversionError ("\x89" from ASCII-8BIT to UTF-8)
/var/www/discourse/lib/theme_store/tgz_exporter.rb:45:in `write'

Same thing one minute laster…

Processing by Admin::ThemesController#export as */*
  Parameters: {"api_key"=>"[FILTERED]", "id"=>"74"}
  Rendering text template
  Rendered text template (0.0ms)
Sent data discourse-custom-signup-and-login--cli-version-.tar.gz (0.5ms)
Completed 200 OK in 105ms (Views: 0.4ms | ActiveRecord: 34.6ms)

Note that I have one theme called “Cakeday Customizations” that’s only 20K and it worked the first time… But the other themes took longer… For example one called “Category Groups Customization” is 412K and I got several error 500’s before the download completed.

Are you connecting to a local development instance, or a production instance?

Are you able to share the very large 412K theme?

2 Likes

Connecting to a production instance.

I can send to you via PM… Just a moment.

2 Likes

I want to use this, but when I try to install on my local computer, I get this after installing ruby:

root@God:~# gem install discourse_theme
Building native extensions. This could take a while...
ERROR:  Error installing discourse_theme:
        ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.5.0/gems/ffi-1.10.0/ext/ffi_c
/usr/bin/ruby2.5 -r ./siteconf20190319-422-z057a8.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /var/lib/gems/2.5.0/gems/ffi-1.10.0 for inspection.
Results logged to /var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/ffi-1.10.0/gem_make.out
root@God:~#1

That looks like an issue with your ruby installation. Can you install other gems correctly?

You might want to look into upgrading your Ruby version as well, we recommend 2.6.1 for dev environments now.

2 Likes

I tried.

This is what I get now:

root@God:~# gem install discourse_theme
Building native extensions. This could take a while...
ERROR:  Error installing discourse_theme:
        ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.5.0/gems/ffi-1.10.0/ext/ffi_c
/usr/bin/ruby2.5 -r ./siteconf20190319-132-yb24z9.rb extconf.rb
checking for ffi.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/$(RUBY_BASE_NAME)2.5
        --with-ffi_c-dir
        --without-ffi_c-dir
        --with-ffi_c-include
        --without-ffi_c-include=${ffi_c-dir}/include
        --with-ffi_c-lib
        --without-ffi_c-lib=${ffi_c-dir}/lib
        --with-libffi-config
        --without-libffi-config
        --with-pkg-config
        --without-pkg-config
/usr/lib/ruby/2.5.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
        from /usr/lib/ruby/2.5.0/mkmf.rb:590:in `try_cpp'
        from /usr/lib/ruby/2.5.0/mkmf.rb:1097:in `block in have_header'
        from /usr/lib/ruby/2.5.0/mkmf.rb:947:in `block in checking_for'
        from /usr/lib/ruby/2.5.0/mkmf.rb:350:in `block (2 levels) in postpone'
        from /usr/lib/ruby/2.5.0/mkmf.rb:320:in `open'
        from /usr/lib/ruby/2.5.0/mkmf.rb:350:in `block in postpone'
        from /usr/lib/ruby/2.5.0/mkmf.rb:320:in `open'
        from /usr/lib/ruby/2.5.0/mkmf.rb:346:in `postpone'
        from /usr/lib/ruby/2.5.0/mkmf.rb:946:in `checking_for'
        from /usr/lib/ruby/2.5.0/mkmf.rb:1096:in `have_header'
        from extconf.rb:16:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/ffi-1.10.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /var/lib/gems/2.5.0/gems/ffi-1.10.0 for inspection.
Results logged to /var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/ffi-1.10.0/gem_make.out
root@God:~#

That’s the likely issue. If you’re on a Mac, you’ll need to install the dev tools, via xcode-select --install.

3 Likes

I installed via Ubuntu on Windows.

I did:

apt-get install ruby-dev

It installed, and still have the issue.

Hmm, you might want to look up how to install Ruby for your specific Ubuntu version.

So much work! I’ll figure it out later :stuck_out_tongue:

oh, looks like you are missing libffi-dev try:

sudo apt-get install libffi-dev

Note you can just install ruby on windows direct and it should work … I recall testing it.

2 Likes

Ruby direct, as in the windows installer?