Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Reload Template Files

Tags:

grails

I have a pretty standard 2.0.3 Grails app and I've executed grails install-templates which places files list.gsp, edit.gsp, etc. in src/templates/scaffolding/ directory. These files are not automatically reloaded when a change is made to them. Is there a way I can get these to be automatically reloaded so I don't have to stop/start the app every time I make a change? I've tried looking at watchedResources but that seems to be plugin development related.

like image 394
Jarred Olson Avatar asked Aug 08 '12 21:08

Jarred Olson


People also ask

How do you refresh a flask template?

Flask does not reload the template when it is changed. After you change a template, you need to reload the web app for it to get the new template.

How do you turn on hot reload on a flask?

Enable Flask development mode By default, flask run launches a production-friendly server process. However, you can opt into a hot-reloading debug mode by setting the FLASK_ENV environment variable. Now, changing any python source files will automatically restart the flask process.

What does lazy loading mean in flask?

Lazy loading is the practice of delaying load or initialization of resources or objects until they're actually needed to improve performance and save system resources.

How do I rerun flask app?

To run the application, use the flask command or python -m flask . You need to tell the Flask where your application is with the --app option. As a shortcut, if the file is named app.py or wsgi.py , you don't have to use --app . See Command Line Interface for more details.


3 Answers

You are correct that the "watched-resources" mechanism only applies to plugins. The correct fix for this would be to modify the core ScaffoldingGrailsPlugin.groovy to add

def watchedResources = "file:./src/templates/scaffolding/*"

and it's probably worth submitting a JIRA to that effect. In the mean time, you may be able to get it working by writing a simple plugin of your own to "inject" this behaviour into the scaffolding plugin. Do grails create-plugin watch-scaffolding and then use the following for the plugin descriptor:

import org.codehaus.groovy.grails.plugins.GrailsPlugin

class WatchScaffoldingGrailsPlugin {
    def version = "0.1"
    def grailsVersion = "2.0 > *"
    def dependsOn = [:]
    def pluginExcludes = [ "grails-app/views/error.gsp" ]

    def title = "Watch Scaffolding Plugin"
    def author = "Your name"
    def authorEmail = ""
    def description = '''\
Watches for changes to scaffolding templates and reloads dynamically-scaffolded
controllers and views.
'''
    // URL to the plugin's documentation
    def documentation = "http://grails.org/plugin/watch-scaffolding"

    // watch for changes to scaffolding templates...
    def watchedResources = "file:./src/templates/scaffolding/*"

    // ... and kick the scaffolding plugin when they change
    def onChange = { event ->
        event.manager.getGrailsPlugin('scaffolding').notifyOfEvent(
            GrailsPlugin.EVENT_ON_CHANGE, null)
    }

    // rest of plugin options are no-op
    def onConfigChange = { event -> }
    def doWithWebDescriptor = { xml -> }
    def doWithSpring = { }
    def doWithDynamicMethods = { ctx -> }
    def doWithApplicationContext = { applicationContext -> }
    def onShutdown = { event -> }
}

Now in your application's BuildConfig.groovy add

grails.plugin.location.'watch-scaffolding' = '../watch-scaffolding'

(or whatever is the appropriate relative path from the root of your app to the root of the plugin) and your scaffolding template changes should start to reload automatically.

(This is tested on Grails 2.1, I initially tried using influences but it didn't have any effect, however forcing an onChange event in the scaffolding plugin had the required result.)

like image 152
Ian Roberts Avatar answered Oct 21 '22 02:10

Ian Roberts


This code flushes scaffolding cache. You can create a specific admin action for it:

org.codehaus.groovy.grails.scaffolding.view.
  ScaffoldingViewResolver.scaffoldedViews.clear()
like image 32
Victor Sergienko Avatar answered Oct 21 '22 02:10

Victor Sergienko


According to GRAILS-755, this has been fixed, but I don't think it has because they don't reload for me either.

From that Jira, here is a possible workaround:

Use the console plugin, and run this command to clear the dynamic scaffolded view cache:

​def scaffoldedView = org.codehaus.groovy.grails.scaffolding.view.ScaffoldingViewResolver.scaffoldedViews.clear()​

After that, the next time I request a page, it doesn't find it in the cache, and thus goes back to the disk to recreate it.

like image 2
cdeszaq Avatar answered Oct 21 '22 02:10

cdeszaq