Write ruby code to change ruby code

13c60e2a5806406b537daa542d3495da?s=47 flyerhzm
September 19, 2014

Write ruby code to change ruby code

13c60e2a5806406b537daa542d3495da?s=128

flyerhzm

September 19, 2014
Tweet

Transcript

  1. Write ruby code to change ruby code Richard Huang Xinmin

    Labs
  2. About Me Founder at Xinmin Labs Full Stack Developer Open

    Source Enthusiast @flyerhzm
  3. Problem

  4. Hey, let’s use rspec new syntax for project A Yes,

    sure, I love the rspec new syntax
  5. Hey, let’s use rspec new syntax for project B Hmmm…ok

  6. Hey, let’s use rspec new syntax for project C Oh,

    no, I don’t want to repeat it again
  7. Developers love upgrading and refactoring code

  8. Developers hate changing code again and again

  9. Automation

  10. Metric Tools

  11. Metric Tools • cane - fails your build if code

    quality thresholds are not met • reek - is a tool that examines Ruby classes, modules and methods and reports any code smells it finds. • rails_best_practices - is a tool to check the quality of rails code. • ……
  12. Tell you what to do, but can’t do for you

  13. IDE

  14. IDE • can rename class / method / variable name

    • can extract methods • can ……
  15. IDE • can’t customize

  16. Based on text

  17. Based on text Replace FactoryGirl.create with create ! $ sed

    -i '' 's/FactoryGirl.create/create/g' spec/**/*.rb
  18. Based on text Replace FactoryGirl.create with create ! !

  19. Based on text Hard to be accurate ! ! !

    !
  20. Based on text Hard to convert complicated cases ! !

    ! !
  21. Based on AST

  22. What’s AST AST is short for Abstract Syntax Tree

  23. What’s AST

  24. Why AST It is much more accurate to parse ruby

    code.
  25. None
  26. None
  27. How to use AST • ripper • ParseTree - https://github.com/seattlerb/parsetree

    • ruby_parser - https://github.com/seattlerb/ruby_parser • parser - https://github.com/whitequark/parser
  28. How to use AST

  29. Example transpec - https://github.com/yujinakayama/ transpec

  30. None
  31. Based on AST Accurate, but hard to write and read

  32. Synvert https://github.com/xinminlabs/synvert

  33. Demo http://xinminlabs.github.io/synvert/

  34. None
  35. Synvert • Add useful attributes to AST nodes ! •

    Provide DSL to parse and rewrite code
  36. Attributes for AST node

  37. Attributes for AST node

  38. Attributes for AST node

  39. Attributes for AST node Check out more attributes http://xinminlabs.github.io/synvert/rules/

  40. DSL • within_file / within_files • with_node / within_node •

    if_exist_node / unless_exist_node • append / insert / insert_after / replace_with / remove • ……
  41. DSL Check out more DSL http://xinminlabs.github.io/synvert/dsl/

  42. None
  43. Playground http://synvert-tools.herokuapp.com/

  44. Snippets https://github.com/xinminlabs/synvert-snippets

  45. Want more? • Fork and send pull request • Open

    issues on github • Hire us to refactor your project
  46. Q&A ! Thank you