Wednesday, June 25, 2014

Solving Error: “syntax error near unexpected token”

Detail Error:
: command not found
: command not found
'/build.sh: line 3: syntax error near unexpected token `{
'/build.sh: line 3: `function stop_channels(){

was thrown when I run
./build.sh

The issue was I opened the .sh (shell script) in Windows OS and Windows changed the end of the line to CRLF

I found this out by running

cat --show-nonprinting filename.extension
E.g.

cat --show-nonprinting test.sh

Or open the file in Notepad++ and click on the button circled in the figure below.

windows vs linux.PNG

Then, I converted it using

dos2unix filename.extension
[Install dos2unix if required]
E.g.

dos2unix myfile.txt
dos2unix myshell.sh



Thursday, June 12, 2014

Android: Solving "Error:The SDK Build Tools revision (19.0.1) is too low. Minimum required is 19.1.0"

After upgrading Android Studio to 0.6.0, I got this error

"Error:The SDK Build Tools revision (19.0.1) is too low. Minimum required is 19.1.0"

Solution:
Open build.gradle file in your project and change the buildToolsVersion from 19.0.1 to 19.1.0 like:

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'
}

Then, Open Android SDK Manager and update the SDK Build-tools by installing version or revision (Rev.) 19.1.

Android SDK Manager: Installing Android SDK Build-tools (Rev. 19.1)

Using Custom Fonts in Grails

I want to include a custom font and I created a folder under web-app and named it 'fonts' and I copied my font to that folder. Then,

1. I added the fonts folder to be included as a grails resource in Config.groovy like:

 // What URL patterns should be processed by the resources plugin  
      grails.resources.adhoc.patterns = ['/images/*', '/css/*', '/js/*', '/plugins/*', '/fonts/*']  
     grails.resources.adhoc.includes = ['/images/**', '/css/**', '/js/**', '/plugins/**', '/fonts/**']  

2. Since I'm using Spring security plugin for grails, I added the 'fonts*' folder to 'permitAll' category of Requestmap.

  // Request map Stored in Database

 for (String url in [  
      '/', '/index', '/index.gsp', 'favicon.ico',  
      'js*', 'css*', 'images*', 'fonts*',  
      '/login', '/login*//**',  
       '/logout', '/logout*//**'  
      ]) {  
        
      Requestmap.findOrSaveByUrlAndConfigAttribute(url, 'permitAll').save()  
 }  

Tuesday, June 10, 2014

Grails: Listing all fields/variables in a Domain/Model

In this example, I assume your domain class name is 'Research' and the full package name is 'com.package.name.Research'

Way 1:

 Research.declaredFields.each{  
      if(!it.synthetic)  
           println it.name  
 }  

Way 2:

 def names = grailsApplication.getDomainClass('com.package.name.Research').persistentProperties.collect {   
           println it.name   
      }  
   
Way 3:

 def Research domainClass = grailsApplication.getGrailsDomainClass('Research')  
 def persistentProperties = domainClass.getPersistentProperties()  
 persisentProperties.each { property ->  
      println property  
 }  
   

Thursday, June 5, 2014

Run Windows Command prompt (CMD) in Full screen mode

I got this values for running Windows CMD in full screen mode. Width=237, Height=82

Open CMD, Right click on the title bar and select Properties and set this values.

For more info, read this.

Android Tips: How to (always) show Overflow in Action bar

Here is how!

try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField =                        ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if(menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Tuesday, June 3, 2014

Parsing and Formatting a Date in Groovy/Grails

If you have a string variable containing a date value such as 2014-06-03T11:03:44.779+03, then:

First -  Convert it to Date using "parse" method of SimpleDateFormat
Seond - Format the Date using the format you want such as "yyyy-MM-dd" or "yyyy/MM/dd" or any other.

Code

import java.text.SimpleDateFormat
import java.text.DateFormat

String mydate = "2014-06-03T11:03:44.779+03"

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd")

Date parsedDate = formatter.parse(mydate)

Date formattedDate = formatter.format(parsedDate)

/* In one line
    Date parsedAndFormattedDate  =  formatter.format(formatter.parse(mydate))
*/